The following code fails to link due to an undefined reference:
// file.h
struct S {
static constexpr auto x = 0;
};
// file.cpp
int main() {
auto const & ref = S::x;
}
Following the advice of Why doesn't the C++11 'auto' keyword work for static members?, this seems to work with clang 3.5:
// file.h
struct S {
static constexpr auto x = 0;
};
// file.cpp
constexpr decltype(S::x) S::x;
int main() {
auto const & ref = S::x;
}
Is it actually valid C++? This seems to violate the rule of "auto everywhere or nowhere" that functions follow (you can forward declare a function that returns auto and then define it to return auto, but you cannot mix auto with non-auto).