Why do I need to mark functions that satisfy the criteria for constant-expression as constexpr
to actually use them as constant expression?
Take this example (from here):
template<int N>
class list
{ };
constexpr int sqr1(int arg)
{ return arg * arg; }
int sqr2(int arg)
{ return arg * arg; }
int main()
{
const int X = 2;
list<sqr1(X)> mylist1; // OK: sqr1 is constexpr
list<sqr2(X)> mylist2; // wrong: sqr2 is not constexpr
}
why do I have to mark sqr2
as constexpr
? Isn't the compiler able to understand that that function satisfies the criteria for constant-expression? If it does the check for sqr1
, why can't it do it for sqr2
and allow sqr2
to be used as a constant expression even without explicitly marking it?