constexpr int hello(int j) {
return j * 12;
}
constexpr int bye(int j = 6) {
return j * 12;
}
int main() {
int i = 6;
constexpr int a1 = hello(i); //error
constexpr int a2 = hello(6); //ok
constexpr int a3 = hello(int(6)); //ok
constexpr int a4 = bye(); //ok
constexpr int a5 = bye(i); //error
constexpr int a6 = bye(6); //ok
return 0;
}
What's difference between hellow(i)
and hello(6)
? I think one is int j = i;
and j is not a constexpr, while the other is int j = 6
and j is still not a constexpr, both j are int
type.
int * literal != constexpr, so the return type is not a constexpr.
I got the above conclusion from an example in the book:
int staff_size = 27; //staff_size is not a const expression
Although staff_size is initialized from a literal, it is not a constant expression because it is a plain int, not a const int.
Moreover, I notice that hello(int(6))
works fine too, what's the "thing" here?
Additionally, bye()
works while hello(i)
does not, both parameter are initialized inside the function, only one with default value, what's the point here?
Very confused, hope someone could explain :D
PS: have no idea to figure out a better title, sorry for that