'struct with most special members defaulted except `constexpr A& operator=(A&& rhs)` fails to compile
Why does this c++ code fail to compile? It's a simple struct with all the special member functions except basic ctors defaulted except for the rref assignment operator when implemented with a simple std::swap Note that it works fine if the copy assignment is defaulted. I can't figure out what the difference is.
Also, the MSVC compiler errors out with a constexpr recursion limit.
failure was caused by evaluation exceeding call depth limit of 512 (/constexpr:depth<NUMBER>)
https://godbolt.org/z/jGK6hbxco
#include <memory>
#include <iostream>
#include <string>
#include <algorithm>
struct A
{
int i;
std::string s;
constexpr A() : i(0), s{ "12345678901234567890" } {}
constexpr A(int ii) : i(ii), s{ "12345678901234567890" } {}
constexpr A(const A&) = default;
constexpr A(A&&) = default;
constexpr A& operator=(const A&) = default;
constexpr A& operator=(A&& rhs)
{
std::swap(*this, rhs);
return *this;
}
constexpr ~A() = default;
};
constexpr int foo()
{
A i0(0); // call ctor
i0 = A(3); // call assign ctor
return i0.i;
}
int main() {
constexpr int i = foo();
return i;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
