skip to Main Content

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

2

Answers


  1. 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

    Login or Signup to reply.
  2. I believe the problem is that you defaulted move constructor of D is private. Try to make it public.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search