I have been looking at the new constexpr
feature of C++ and I do not fully understand the need for it.
For example, the following code:
constexpr int MaxSize()
{
...
return ...;
}
void foo()
{
int vec[MaxSize()];
}
can be replaced by:
int MaxSize()
{
...
return ...;
}
static const int s_maxSize = MaxSize();
foo()
{
int vec[s_maxSize];
}
Update
The second example is actually not standard ISO C++ (thanks to several users for pointing this out) but certain compilers (e.g. gcc) support it. So it is not const
that makes the program valid, but the fact that gcc supports this non-standard feature. (To my knowledge, this is possible only when the array is defined as local to a function or method, since the size of a global array must still be known at compile time.) If I compile without the options -std=c++98 -pedantic-errors
, even the code
int MaxSize()
{
return 10;
}
void foo()
{
int vec[MaxSize()];
}
will compile with gcc.
So I will try to rephrase my question taking into account the feedback that came so far (and also some further reading I have done in the mean time).
I use the const
keyword heavily. With const
I can define a constant that has a certain value during its whole lifetime. A constant can be initialized with any expression, which is evaluated once, namely when the constant is created. For these cases, I think that constexpr
is pretty useless: it would introduce a very small optimization in that the expression defining the constant value would be computed at compile time instead of run time. Every time I need a run-time constant with a complex initialization I use the keyword const
.
So constexpr
may come in handy in situations where we need to initialize a constant at compile time. One example is a vector definition: the standard does not support defining the size at runtime. Another example is a template with one or more non-type parameters.
In such cases I normally use macros:
#define MAX_SIZE (10)
void foo()
{
int vec[MAX_SIZE];
}
but, if I understand correctly, constexpr
functions are more powerful than macros, since they allow recursive calls of constexpr
functions in their definition. However, I cannot think of any practical application in which I ever wanted to use such a complex computation to define a compile-time constant.
So, even if it may be an interesting feature, I still wonder if it is needed (i.e. how often it can solve situations where macros are not enough). Maybe looking at a few real-life examples that cannot be solved with macros would help me to change this opinion.
See Question&Answers more detail:os