c++ news Does any compiler support constexpr yet?

visual studio 2017 c++ support (4)

I want to play with constexpr, does any compiler support it yet?

Answer #1

As of July 2011, gcc 4.7 supports constexpr. You need to build it from svn though.

Answer #2

Agreed, g++ 4.5 and 4.6 support the keyword, but ignore the implications. I just compiled a simple factorial program (on both versions using -std=c++0x) with the line:

constexpr fact(int i) { return (i>1) ? fact(i-1)*i : 1; }

and it compiled and ran but when examining the asm source (-S option) it showed the function was called with the parameter instead of being determined by the compiler.

Answer #3

The Apache Stdcxx project has a nice table detailing which C++0x features are supported by which compilers. It's been updated on a regular basis and covers most of the modern C++ compilers.

According to that, only GCC 4.5 supports constexpr (note that that support may be experimental).

Between that list and what has been said in the comments, it appears the answer is "no."

Answer #4

Usage of "constexpr" is really easy. Look at this piece of code:

constexpr int get_five(){
return 5;}

This function returns always 5, so it can be declared with "constexpr" keyword. But factorial function returns value depending on argument, so its "output" is not always the same.