1

One thing I know,may not be true, is that the T should be copy-constructible, that is, the T should have an accessible copy constructor.

However,is there any other requirements,like copy assignable?

As a complement, I remember that the Effective STL says vector<bool> is not a standard container because it doesn't meet the requirement of T *p=&c[0] being well-formed.

choxsword
  • 3,187
  • 18
  • 44
  • 1
    Are you referring to [this](https://timsong-cpp.github.io/cppwp/container.requirements)? and what type of container are you asking? – Joseph D. May 31 '18 at 05:59
  • @codekaizer Rules for all types of containers. For example, all containers require `T` to be **copy constructible**. – choxsword May 31 '18 at 06:01
  • 5
    It is not true that all containers require T to be copy constructible – M.M May 31 '18 at 06:01
  • @M.M I read that requirement from [this question](https://stackoverflow.com/questions/6532173/copyconstructible-requirement-for-c-stl-container-element). – choxsword May 31 '18 at 06:02
  • @bigxiao the asker of that question incorrectly cites the standard – M.M May 31 '18 at 06:04
  • @M.M What does standard actually say about that ? I found the requirement of **copy-insertable** from [cppref](https://en.cppreference.com/w/cpp/concept/Container)(At the bottom of the linked page) . And I thought **copy-insertable** == **copy-constructible**. – choxsword May 31 '18 at 06:06
  • @bigxiao, CopyInsertable is a different concept. It is not part of the requirments of the `value_type` of Containers. – R Sahu May 31 '18 at 06:09
  • @RSahu In [this page](https://timsong-cpp.github.io/cppwp/container.requirements) it seems that since the standard require that every container should have `X(a)`, so `T` should be **copy insertable**. – choxsword May 31 '18 at 06:12
  • @codekaizer Does all standard containers need to meet the requirements and implement all APIs in that table? – choxsword May 31 '18 at 06:16
  • 2
    @bigxiao I think you misinterpret the Standard. It effectively says that the expression `X(a)` is well formed, if `T` is `CopyInsertable` into `X`. If not, you cannot copy-construct a container `X`. A trivial example: `std::vector v1; std::vector v2(v1);` throws an error, since `std::thread` has deleted copy constructor. Thus, you can have a `vector` of `thread`s, but you cannot copy such a vector. – Daniel Langr May 31 '18 at 06:24
  • @DanielLangr So does that mean there are no compulsive requirements for all containers? – choxsword May 31 '18 at 06:28
  • @bigxiao No, I just responded to your comment. However, I believe there are some requirements common to all the containers from Standard library. At least those that have no _Assertion/note pre-/post-condition_ in the table. Such as expression `a.begin()`; all the containers must implement member function `begin`, which you can consider as a _requirement_. – Daniel Langr May 31 '18 at 06:30

1 Answers1

5

I think there are different requirements for sequence containers and associative containers. For example, following is for vector(see cppreference.com for more details). Also, note that it has been changed with newer version of c++.

  • T must meet the requirements of CopyAssignable and CopyConstructible. (until C++11)

  • The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type is a complete type and meets the requirements of Erasable, but many member functions impose stricter requirements. (since C++11) (until C++17)

  • The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type meets the requirements of Erasable, but many member functions impose stricter requirements. This container (but not its members) can be instantiated with an incomplete element type if the allocator satisfies the allocator completeness requirements. (since C++17)

code707
  • 1,663
  • 1
  • 8
  • 20
  • Is copyconstrucible==copyinsertable? – choxsword May 31 '18 at 06:29
  • 3
    @bigxiao If they were, they wouldn't be two different concepts. With `CopyInsertable` concept, allocators come into play. See this answer for some details: https://stackoverflow.com/a/14916142/580083. – Daniel Langr May 31 '18 at 06:38