Std :: is_copy / move_constructible doesn't work even though default copy / move constructors

I have an Input class that has default move / copy constructors.

Input(const Input &) = default;
Input(Input &&) = default;

      

However, the following statements do not hold.

static_assert(std::is_copy_constructible<Input>(), "Input is not copy-constructible");
static_assert(std::is_move_constructible<Input>(), "Input is not move-constructible");

      

Why is this?

Here's a complete example:

#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;
}

      

+3


source to share


4 answers


Your problem is what static_assert

is in the class declaration. The compiler cannot know exactly when it will reach static_assert

whether the class is constructive with respect to copying or moving because the class is not fully defined yet.



+8


source


The problem is that you have a test inside the class itself. To evaluate static_assert

, the compiler must complete the class. This is impossible, because it requires an assessment static_assert

. This is a chicken and egg problem.



+5


source


This piece of code ( live on Ideone ) works fine:

#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>(), "");
}

      

+1


source


In your example, you have implicitly declared your constructors as private (default accessibility in class types).

If this is in your real code as well, it could be a problem.

0


source







All Articles