如何在静态上声称一个物体可以移动
为了实现容器类,该类别将原始指针保持在对象上(暂时计算和枚举对象的子集),我想静态地断言,指向的对象无法移动以确保指示器至少对指针保持有效性至少有效对象的寿命。
例如,如果指向对象可移动并活在向量中,则它们可能会在向量生长或重组时移动。
虚弱的指针基本上会完成这项工作,但是要求所有对象共享的目的是过度杀伤。我还想在不检查空指针的情况下迭代容器,尤其是因为其中的对象数量是重要的信息。
需要满足以下条件之一:
a)物体生存在堆上(前提是在容器之前未删除它们)。
b)物体生活在堆栈上(提供的容器生活在相同的范围或嵌套范围内)。
c)对象不能按照类别的定义移动(前提是它们处于相同或更高的范围)。
可以使用std :: is_move_constructible
和喜欢的情况来验证案例C,尽管我不确定是否足够。还有一种断言A和B的方法吗?
For an implementation of a container class that keeps raw pointers to objects (intended for computing and enumerating subsets of objects temporarily), I want to statically assert that the objects pointed to can't be moved to assure that pointers remain valid at least for the lifetime of the objects.
For example, if the objects pointed to are moveable and live in a vector, they might move when the vector grows or reorganizes.
Weak pointers would basically do the job, but requiring all objects to be shared is overkill for the purpose. I also want to iterate the container without checking for null pointers, especially as the number of objects in it is an important information.
One of the following conditions need to be met:
A) Objects live on the heap (provided they are not deleted before the container).
B) Objects live on the stack (provided container lives in the same scope or a nested scope).
C) Objects can't be moved according to class definition (provided they live in the same or a higher scope).
Case C can be verified with std::is_move_constructible
and the likes, although I'm not sure if that suffices. Is there also a way to assert A and B?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论