For the following code, why does the first case in main work fine without the redeclaration of Foo::bar, whereas the second case with the function requires it?
struct Foo{
static constexpr int bar = 30;
};
//Declaration of Foo::bar outside of struct
constexpr int Foo::bar;
int returnconstexpr(const int& x) { return x; }
int main()
{
//Ok without declaration outside of struct
std::cout << Foo::bar << std::endl;
//Requires declaration outside of struct
std::cout << returnconstexpr(Foo::bar) << std::endl;
//Here static constexpr works as a definition
static constexpr int x = 2;
std::cout << returnconstexpr(x) << std::endl;
return 0;
}
I am assuming this is because in the first case, the compiler literally just sticks in the value, whereas in the second case the function requires an address which doesn't yet exist without the redeclaration. If that is so, then is the the thing I'm saying is declaration actually a definition? I am confused by this because an initialiser is provided in the class, but it doesn't make it a definition. For example, the third case works just fine.