constexpr question?

Hello, I have just started to learn about constexpr. I found code online for a function using constexpr so I copied it (first code shown below) but my VS was giving a warning saying that int A, should instead be declared as a constexpr, rather than the const. But I found this code online at geeksforgeeks, which I normally find a very good, educational site. I believe the code I found is for c++11 and I believe there has been an update for the constexpr in c++14 (dont know if update if the correct word), so I am not sure if that is why my compiler is complaining.

So, what is the correct way here, and is there a big difference between using const and constexpr when it comes to int A, and variables in general?


Original code
1
2
3
4
5
6
7
8
9
10
11
  constexpr int product(int x, int y) 
{ 
    return (x * y); 
} 
  
int main() 
{ 
    const int x = product(10, 20); 
    cout << x; 
    return 0; 
} 


updated code with constexpr

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

constexpr int product(int x, int y)
{
	return (x * y);
}

int main()
{

	static_assert(product(10, 20) == 200, "Wrong\n");
	constexpr int A = product(10, 20);
	std::cout << A;
	return 0;
}

items defined as constexpr will be executed at compile time if possible. If they cannot be evaluated at compile time then they will be executed at run-time. Marking product as constexpr means thatthe compiler will attempt to execute at compile time (ie in this case if x and y are known at compile time). Having expressions evaluated at compile time helps to produce faster program execution at run-time with a (small?) overhead in compile time. In this case the difference won't be noticable b ut for complicated expressions/functions it can be significant.

A is required to be constexpr so that the result of product(), if evaluated at compile time as here can be assigned to A at compile time. If A isn't constexpr then A will be initialised at run-time and hence product() evaluated at run-time rather than compile-time.

Note that in C++20 there are also constinit and consteval.

Also note that there has been much work in C++20 to make as much of the STL constexpr as possible.

See https://en.cppreference.com/w/cpp/language/constexpr
and links.
MS writes in https://docs.microsoft.com/en-us/cpp/cpp/constexpr-cpp?view=msvc-160
The primary difference between const and constexpr variables is that the initialization of a const variable can be deferred until run time. A constexpr variable must be initialized at compile time. All constexpr variables are const.

The "the initialization of a const variable can be deferred until run time", if valid, implies:
1
2
3
4
5
6
7
int test() { int x; std::cin >> x; return x; }

int main() {
  constexpr int aa = 42; // will be initialized during compilation
  const int bb = 42; // can be initialized during compilation or during run-time
  const int cc = test(); // must be initialized during run-time
}

If the implementation of bb's initialization is up to compiler, then we don't know.
It is nice that your compiler points that out for you.
It is not nice that geeksforgeeks has it in their example about compilation time evaluation.
Topic archived. No new replies allowed.