I have learnt about constexpr
variables in c++ and read that int i =0; constexpr int j = i;
fails because i
is not a constant expression which makes sense. But then when I did the same with a variable of a class, it worked.
struct C{};
int main()
{
int i = 0; //i is not a constant expression as expected
//constexpr int j = i; //this fails AS EXPECTED
C c;
constexpr C d = c; //WHY DOESN'T THIS FAIL??
}
As you can see, constexpr C d = c;
compiles without any problem unlike constexpr int j = i;
even though c
is also not a constant expression.
I want to know the reason behind this.