英文:
What should be the definition for the concept of `vector` element type?
问题
我正在尝试实现vector
以练习C++并使用C++20的concept
来约束vector
的元素类型。
对于vector
元素类型,concept
(类型约束)的定义应该是什么?我认为std::destructible
和std::move_constructible
是必要的,还有其他什么吗?顺便问一下,使用concept
来约束vector
的元素类型是一个不好的想法吗?
英文:
I am trying to implement vector
to practice C++ and use C++20 concept
to constrain vector
's element type.
What should be the definition of concept
(type constraints) for vector
element type? I think std::destructible
and std::move_constructible
are necessary, is there any thing else? And by the way, is using concept
to constrain vector
's element type a bad idea?
答案1
得分: 2
根据https://en.cppreference.com/w/cpp/container/vector:
对元素所施加的要求取决于容器上执行的实际操作。通常需要元素类型满足可抹除的要求,但许多成员函数会施加更严格的要求。如果分配器满足分配器完整性要求,可以使用不完整元素类型来实例化此容器(但不适用于其成员)。
一般的最低要求是可抹除的,请参阅https://stackoverflow.com/questions/60449592/how-do-you-define-a-c-concept-for-the-standard-library-containers/60450396#60450396了解其实现。
使用concept
来指定这个最低要求是个坏主意,因为它不能与不完整类型一起工作。
英文:
According to https://en.cppreference.com/w/cpp/container/vector:
> 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.
The general minimum requirement is Erasable and refer to https://stackoverflow.com/questions/60449592/how-do-you-define-a-c-concept-for-the-standard-library-containers/60450396#60450396 for its implementation.
It is a bad idea to use concept
to specify this minimum requirement because it does not work with incomplete type.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论