【发布时间】:2013-08-26 13:55:59
【问题描述】:
类型特征是否应该能够处理std::vector < std::unique_ptr <int> > 之类的情况并检测到它不是可复制构造的?
这是https://ideone.com/gbcRUa 的示例(运行 g++ 4.8.1)
#include <type_traits>
#include <vector>
#include <iostream>
#include <memory>
int main()
{
// This prints 1, implying that it's copy constructible, when it's clearly not
std::cout << std::is_copy_constructible< std::vector<std::unique_ptr<int> > >::value << std::endl;
return 0;
}
如果这是is_copy_constructible 的正确行为,有没有办法检测出复制结构的格式不正确?好吧,不仅仅是让它无法编译。
【问题讨论】:
-
很遗憾,是的,这是允许的行为 :(。我继续讨论这个问题,以及为什么我们需要使用 SFINAE 来防止它,这里:flamingdangerzone.com/cxx11/2013/02/11/…
-
@R.MartinhoFernandes:Bleh,我担心你会这么说。我之前没有找到
is_constructible的问题,但答案是一样的,因为is_copy_constructible指的是is_constructible。 -
是的,这很烦人。您的问题促使我将问题提交到标准提案邮件列表:groups.google.com/a/isocpp.org/d/msg/std-proposals/pgWYGIvV2Tw/…。因为 C++14 提案的截止日期是 8 月 30 日,所以我可能早在 2 月就应该这样做了。
-
@R.MartinhoFernandes:呃,我认为我们可以使用
decltype和一个明确的检查来解决这个问题,但是apparently not,这也是你知道的一个缺陷吗? -
@MatthieuM。是的,这本质上是同一个问题:
decltype也不会超越当前的上下文。真的没有办法不改变vector:(
标签: c++ c++11 typetraits g++4.8