Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a defaulted constructor/assignment noexcept/constexpr by default?

Tags:

c++

c++11

c++14

So, my question is simple:

Is there any point in specifying a defaulted class constructor as noexcept or constexpr (or any other thing you could thing of)?

struct foo
{
   foo() = default;
   // vs
   constexpr foo() noexcept = default;

   // same thing would apply for copy/move ctors and assignment operators
};

Would the two behave the same way?

Does it depend on whether the class is POD? For example with the above example both would behave the same way, while if for example I had a private member std::vector<int> v = { 1, 2, 3, 4 }; which uses in-class assignment, foo() = default; would by default not be noexcept and not constexpr.

By writing foo() = default; does the compiler just pick the best version: noexcept if possible and constexpr if possible, etc?

like image 748
DeiDei Avatar asked Mar 22 '16 17:03

DeiDei


People also ask

Are default constructors 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 ...

Is Constexpr a default constructor?

defaulted default constructor cannot be constexpr because the corresponding implicitly declared default constructor would not be constexpr.

What does Noexcept mean in C++?

noexcept (C++) C++11: Specifies whether a function might throw exceptions.

What is the purpose of Noexcept?

The noexcept operator performs a compile-time check that returns true if an expression is declared to not throw any exceptions. It can be used within a function template's noexcept specifier to declare that the function will throw exceptions for some types but not others.


1 Answers

[dcl.fct.def.default]/2-3:

2 An explicitly-defaulted function that is not defined as deleted may be declared constexpr only if it would have been implicitly declared as constexpr. If a function is explicitly defaulted on its first declaration,

  • it is implicitly considered to be constexpr if the implicit declaration would be, and,
  • it has the same exception specification as if it had been implicitly declared ([except.spec]).

3 If a function that is explicitly defaulted is declared with an exception-specification that is not compatible ([except.spec]) with the exception specification of the implicit declaration, then

  • if the function is explicitly defaulted on its first declaration, it is defined as deleted;

  • otherwise, the program is ill-formed.

In other words, foo() = default;, which is necessarily the first declaration of foo's default constructor, will be "constexpr if possible" and "noexcept if possible". Explicitly writing constexpr and noexcept is still useful; it means "yell at me if it can't be constexpr/noexcept".

like image 196
T.C. Avatar answered Sep 18 '22 13:09

T.C.