std::is_copy/move_constructible失败,即使复制/移动构造函数是默认的
std::is_copy/move_constructible fails even though copy/move constructors are defaulted
我有一个类Input,它有默认的移动/复制构造函数。
Input(const Input &) = default;
Input(Input &&) = default;
但是,以下断言失败了。
static_assert(std::is_copy_constructible<Input>(), "Input is not copy-constructible");
static_assert(std::is_move_constructible<Input>(), "Input is not move-constructible");
为什么?
下面是一个完整的例子:
#include <type_traits>
class A {
public:
A(const A &) = default;
static_assert(std::is_copy_constructible<A>(), "");
};
int main() {
// your code goes here
return 0;
}
您的问题是static_assert
在类声明中。编译器不能确定当它到达static_assert
时,该类是可复制的还是可移动的,因为该类还没有完全定义。
问题是测试在类本身中。为了评估static_assert
,编译器需要完成类。这是不可能的,因为static_assert
需要为此进行评估。这是一个鸡和蛋的问题。
这个代码片段(在Ideone上运行)似乎运行得很好:
#include <type_traits>
class Input {
public:
Input(const Input &) = default;
Input(Input &&) = default;
};
int main() {
static_assert(std::is_copy_constructible<Input>(), "");
static_assert(std::is_move_constructible<Input>(), "");
}
在您的示例中,您隐式地将构造函数声明为private(类类型中的默认可访问性)。
如果在您的真实代码中也是这样,那么这可能就是问题所在。
相关文章:
- 当有分配器意识的容器被复制/移动时,反弹分配器是否被复制/移走
- 为什么复制而不是移动数据元素?
- 使lambda不可复制/不可移动
- 复制和交换习惯用法与移动操作之间的交互
- 为什么调用复制构造函数而不是移动构造函数?
- 隐式移动与复制操作和遏制
- 具有已删除移动和复制构造函数的类的就地构造
- 使用移动和复制语义时函数匹配如何工作?
- 直接初始化不可复制、不可移动的成员,而不使用聚合初始化
- 包装二进制缓冲区 - 可能没有多余的移动/复制?
- 当使用三元运算符并删除移动/复制CTOR时,Visual Studio不执行RVO
- 我们是否应该从派生类调用基类移动复制/分配构造函数
- 具有unique_ptr成员变量的 C++ 移动/复制/赋值<AbstractClass>
- 返回带有已删除移动/复制 ctor 的类型临时
- 消除默认/删除移动/复制语义中涉及的样板的好方法是什么
- C++11:移动/复制构造不明确
- 关于移动/复制c++对象实例
- 我丢失了一个移动/复制赋值
- 如何创建一个不可移动/复制构造的对象数组
- 编译器何时在C++中移动/复制