Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::即使复制/移动构造函数为默认值,是否可复制/移动\u构造函数也会失败_C++_Copy Constructor_C++14_Move Constructor - Fatal编程技术网

C++ std::即使复制/移动构造函数为默认值,是否可复制/移动\u构造函数也会失败

C++ std::即使复制/移动构造函数为默认值,是否可复制/移动\u构造函数也会失败,c++,copy-constructor,c++14,move-constructor,C++,Copy Constructor,C++14,Move Constructor,我有一个类输入,它有默认的move/copy构造函数 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<Inp

我有一个类输入,它有默认的move/copy构造函数

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");
static_assert(std::is_copy_constructible(),“Input is not copy constructible”);
静态_断言(std::is_move_constructible(),“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;
}
#包括
甲级{
公众:
A(常数A&)=默认值;
静态断言(std::is_copy_constructible(),“”);
};
int main(){
//你的密码在这里
返回0;
}
此代码片段()似乎工作正常:

#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>(), "");
}
#包括
类输入{
公众:
输入(常量输入&)=默认值;
输入(输入&&)=默认值;
};
int main(){
静态断言(std::is_copy_constructible(),“”);
静态断言(std::is_move_constructible(),“”);
}

在您的示例中,您隐式地将构造函数声明为private(类类型中的默认可访问性)


如果你的真实代码也是这样,那可能就是问题所在。

你的问题是
静态断言在类声明中。编译器无法确定何时到达
static\u assert
类是可复制还是可移动构造的,因为该类尚未完全定义。

问题是您在类本身中进行了测试。为了计算
静态断言
,编译器需要完成该类。这是不可能的,因为需要对
静态断言
进行评估。这是一个鸡和蛋的问题。

您没有给我们提供足够的信息来回答您的问题。您使用的是哪种编译器?请在您的问题中添加一个。我添加了一个实例。对不起,这是一个输入错误,我编辑了我的实例。它们是公共的。@gartenriese:在您的代码段中,如果您将静态断言移出类定义并移到main中,它似乎工作得很好。