Spurious interaction between noexcept specs. for move/copy assignment
| Bugzilla Link | 38983 |
| Version | unspecified |
| OS | MacOS X |
| CC | @DougGregor,@zygoloid |
Extended Description
The following (contrived) code appears to indicate a spurious dependency between the exception specifications for the move/copy assignment operators.
template<class T>
class wrapper
{
T& m_Ref;
// Reported problem disappears if reference_wrapper is used instead
public:
wrapper(T& ref) : m_Ref{ref} {}
wrapper(const wrapper&) = default;
wrapper(wrapper&&) noexcept = default;
wrapper& operator=(const wrapper&) noexcept(false){}
// 'false' required to reveal bug;
//removing false fixes compilation
wrapper& operator=(wrapper&&) noexcept = default;
};
template<class T, class W>
class thing
{
private:
W m_Member{};
public:
thing(T& ref) : m_Member{ref} {}
thing(const thing&) = default;
thing(thing&&) = default;
thing& operator=(const thing&) = default;
thing& operator=(thing&&) noexcept = default;
};
int x{5};
wrapper<int> w{x}; // fine
thing<int, wrapper<int>> foo{x};
Compiler error message:
exception specification of
explicitly defaulted move assignment operator does not match the
calculated one
thing& operator=(thing&&) noexcept = default;
The last line of code fails to compile due to the exception spec of the move assignment operator not matching the calculated one.
Compilation may be fixed by changing the exception spec. of the copy assignment operator.
Thanks for clarifying that.
I wonder if it would make sense for clang to issue a warning when a special member function is deleted if a user not merely defaults said function but explicitly provides a noexcept specification.
The defaulted move assignment operator of wrapper
As a consequence, overload resolution for the wrapper
So Clang is correct that a defaulted move assignment operator for thing would be noexcept(false).
However...
As of C++ DR 1778 (which clang does not yet implement), the result of this situation was changed from the program being ill-formed to the assignment operator of thing being deleted: http://eel.is/c++draft/dcl.fct.def#default-2.4
... and the reason that Clang does not implement DR 1778 is because it is actually impossible to do so (because it violates necessary invariants by changing which special members are deleted after a class is completed).
@llvm/issue-subscribers-clang-frontend
Author: Oliver Rosten (ojrosten)
Extended Description
The following (contrived) code appears to indicate a spurious dependency between the exception specifications for the move/copy assignment operators.
template<class T>
class wrapper
{
T& m_Ref;
// Reported problem disappears if reference_wrapper is used instead
public:
wrapper(T& ref) : m_Ref{ref} {}
wrapper(const wrapper&) = default;
wrapper(wrapper&&) noexcept = default;
wrapper& operator=(const wrapper&) noexcept(false){}
// 'false' required to reveal bug;
//removing false fixes compilation
wrapper& operator=(wrapper&&) noexcept = default;
};
template<class T, class W>
class thing
{
private:
W m_Member{};
public:
thing(T& ref) : m_Member{ref} {}
thing(const thing&) = default;
thing(thing&&) = default;
thing& operator=(const thing&) = default;
thing& operator=(thing&&) noexcept = default;
};
int x{5};
wrapper<int> w{x}; // fine
thing<int, wrapper<int>> foo{x};
Compiler error message:
exception specification of
explicitly defaulted move assignment operator does not match the
calculated one
thing& operator=(thing&&) noexcept = default;
The last line of code fails to compile due to the exception spec of the move assignment operator not matching the calculated one.
Compilation may be fixed by changing the exception spec. of the copy assignment operator.
Bisected: the reported behavior stops reproducing since https://github.com/llvm/llvm-project/commit/2b45b267dab4
Godbolt: https://godbolt.org/z/3EEr7zzz8
- Clang 8.0.0: Reports
exception specification of explicitly defaulted move assignment operator does not match the calculated oneat line 30 - Clang 9.0.0: No longer reports the exception-specification error
- Clang (trunk): Compiles without the exception-specification error
This is only an observed change in behavior. I have not checked whether the change in this range addresses the cause of this report or only stops it from reproducing; whether that resolves the issue is up to the maintainers.
Prepared with assistance from Codex.