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
Inheriting constructors and the implicitly-declared default constructors, copy constructors, move constructors, destructors, copy-assignment operators, move-assignment operators are all noexcept(true) by default, unless they are required to call a function that is noexcept(false) , in which case these functions are ...
If a copy constructor, copy-assignment operator, move constructor, move-assignment operator, or destructor is explicitly declared, then: No move constructor is automatically generated. No move-assignment operator is automatically generated.
A move constructor enables the resources owned by an rvalue object to be moved into an lvalue without copying. For more information about move semantics, see Rvalue Reference Declarator: &&.
C++11 noexcept KeywordThis keyword can be used for specifying that any function cannot throw — or is not ready to throw. Here is a code snippet: void test() noexcept; This declares that test() won't be able to throw.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With