C++ Why does one need to mark a constant-expression eligible function as constexpr?

Viewed 80

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?

1 Answers

All template parameters must be known at compile time. Template is just recipe to create code. Code is generated when template is instantiated with all required template parameters.

So list<sqr2(X)> fails, since sqr2 can't be evaluated at compile time. Only constexpr (or consteval) functions can be evaluated at compile time.

Related