Re: Can you ever mix an exception spec with default definition
within a class definition block in C++11?
Am 14.03.2012 22:33, schrieb Daryle Walker:
I've heard that you can't do:
class MyClass
{
MyClass() noexcept = default;
//...
};
Someone told you the wrong thing. This defaulted member definition is fine according to C++11, *if* the implicit exception specification is compatible with the declared one, see [dcl.fct.def.default] p2:
"An explicitly-defaulted function [..] may have an explicit exception-specification only if it is compatible (15.4) with the exception-specification on the implicit declaration."
This means that if MyClass would actually be defined as follows:
struct MyMember
{
MyMember(){} // noexcept(false)
}
class MyClass
{
MyClass() noexcept = default;
MyMember member;
};
The defaulted definition would be ill-formed, because there is a conflict between the implicit and the assumed exception specification.
and you have to either drop the exception clause or move the "= default"
outside the class definition (as part of an external constructor
definition). The problem with the latter, IIUC, is that it disqualifies
the class from being trivial. (The default constructor would count as
being user-provided.)
If above code doesn't work on your compiler (which one, by the way, and which version?), I assume that the problem is either related to the fact that you have the conflicting situation mentioned above or you might have an "older" compiler. Note that above mentioned rule was a rather late addition, see
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1135
Actually, the code I had in mind used a conditional noexcept, if that
make a difference.
This *could* make a difference, but we need to see the complete example.
Returning to my previous example the following should be valid, for example:
#include <type_traits>
struct MyMember
{
MyMember(){}
};
class MyClass
{
MyClass() noexcept(std::is_nothrow_default_constructible<MyMember>::value) = default;
MyMember member;
};
Is there any way to have a trivial default constructor
with an exception specification?
Yes, see above.
(This wouldn't be a problem if I had no
constructors, then all the special functions would be defined automatically
and be trivial if possible. But this class will have some configuration
constructors, so I need to explicitly declare a default constructor.) An
alternative would be if the rules of C++ say somewhere that an in-class
defaulted default-constructor isn't always automatically any-throw, but will
secretly have the right throw specification even when omitted.
But the current rules do exactly say that! From the same paragraph:
"If a function is explicitly defaulted on its first declaration,
[..]
? it is implicitly considered to have the same exception-specification as if it had been implicitly declared (15.4)."
Or is there
a conceptual reason that throw specifications and defaulting don't mix?
No, there is no such reason and no such rule.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]