I tested the following code with both clang and gcc (trunk versions). Can someone explain why the case with the plain X struct does not work, while both the by value capture and the wrapper case that uses const reference work just fine.
struct X {
constexpr X(const int &v) : x(v) {}
constexpr int Get() const { return x; }
private:
const int& x;
};
constexpr X f(const X& r) {
return r;
}
struct Y {
constexpr Y(const int &v) : x(v) {}
constexpr int Get() const { return x; }
private:
const int x;
};
constexpr Y f(const Y& r) {
return r;
}
struct Wrap {
constexpr Wrap(const int& v) : x(v) {}
constexpr Y Get() const { return Y{x}; }
private:
const int x;
};
int main() {
constexpr const int x = 10;
/* this does not work for some reason
constexpr X a(x);
static_assert(f(a).Get() == 10, "This should work.");
*/
// This works.
constexpr Y b(x);
static_assert(f(b).Get() == 10, "This should work.");
// This also works.
constexpr Wrap c(x);
static_assert(f(c.Get()).Get() == 10, "This should work.");
return 0;
}