“CopyConstructible” requirement for C++ stl container element

后端 未结 6 1122
后悔当初
后悔当初 2021-01-04 22:05

Regarding to the requirement for C++ stl container element, the standard says: the element type should be CopyConstructible, and there is a table for CopyConstructible requi

6条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-04 22:37

    Thinking that "a copy constructor performs either a deep or a shallow copy" is slightly limiting, and something of a red herring.

    Whilst it's true that, depending on what your object stores as members, you may need to do some deep copying in order to gain equivalence, as far as the type's interface is concerned, it doesn't really matter how you performed the copy... as long as you did perform the copy and you ended up with an equivalent object.

    And if A is equivalent to B, then for a properly-designed type, A==B.

    The entire requirement is just saying: "the element type must be copyable". All the rest is down to usual common sense of writing a proper copy constructor.

提交回复
热议问题