Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my defaulted move constructor not noexcept?

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 
like image 800
Pierre Avatar asked Jan 22 '20 08:01

Pierre


People also ask

Is the default move constructor Noexcept?

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

Are move constructors automatically generated?

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.

What does the default move constructor do?

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: &&.

What is Noexcept keyword in C++?

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.


1 Answers

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

like image 158
songyuanyao Avatar answered Oct 02 '22 16:10

songyuanyao