28

According to the answer of this question, a default move constructor can be defined as noexcept under certain conditions. For instance, the following class generates a noexcept move constructor:

class C {};

According to the answer to this question, a move constructor defined with the = default specifier would generate the same one as an implicitly defined move constructor. So, if I correctly understand it, the following class should generate a noexcept move constructor:

class D {
    D(D&&) = default;
};

To check that, I used the std::is_nothrow_move_constructible function to see if C and D have a noexcept move constructor:

#include <type_traits>

int main() {
    static_assert(std::is_nothrow_move_constructible<C>::value, "C should be noexcept MoveConstructible");
    static_assert(std::is_nothrow_move_constructible<D>::value, "D should be noexcept MoveConstructible");

    return 0;
}

When I compile, I get this error:

$ g++ toy.cpp -o toy
toy.cpp: In function ‘int main()’:
toy.cpp:16:5: error: static assertion failed: D should be noexcept MoveConstructible
     static_assert(std::is_nothrow_move_constructible<D>::value, "D should be noexcept MoveConstructible");
     ^~~~~~~~~~~~~

Why is my D move constructor not noexcept?


I also tried with Clang and I get the same error. Here is the information about my compilers:

$ g++ --version
g++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ clang++8 --version
clang version 8.0.0 
Target: x86_64-unknown-linux-gnu
Thread model: posix
Boann
  • 48,794
  • 16
  • 117
  • 146
Pierre
  • 1,942
  • 3
  • 23
  • 43

2 Answers2

33

In fact it has nothing to do with noexcept; static_assert would fail also with std::is_move_constructible because the move constructor is private. So just declare it as public.

class D {
public:
    D(D&&) = default;
};

LIVE with Clang8

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
13

I believe the problem is that you defaulted move constructor of D is private. Try to make it public.

Daniel Langr
  • 22,196
  • 3
  • 50
  • 93