`vector` 元素类型的定义应该是什么?

huangapple go评论82阅读模式
英文:

What should be the definition for the concept of `vector` element type?

问题

我正在尝试实现vector以练习C++并使用C++20的concept来约束vector的元素类型。

对于vector元素类型,concept(类型约束)的定义应该是什么?我认为std::destructiblestd::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.

huangapple
  • 本文由 发表于 2023年8月5日 00:09:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定