constexpr is a modifier introduced in C++11, which informs the compiler that the value of a function or variable is known or can be calculated at compile time. As such, it can be used as a constant in places where otherwise it couldn't be.
Questions tagged [constexpr]
2435 questions
47
votes
4 answers
constexpr not compiling in VC2013
This constexpr code does not compiled in Visual Studio 2013 version 12.0.21005.1 REL
Is there a newer Visual Studio compiler that works with constexpr?
#include
constexpr int factorial(int n)
{
return n <= 1 ? 1 : (n * factorial(n -…

Damian
- 4,395
- 4
- 39
- 67
46
votes
3 answers
inline vs. constexpr?
With the new C++11 standard, when should I use the inline keyword over the constexpr keyword? Does the constexpr keyword offer any additional optimization over inline, or does it merely assert that things must be computed at compile-time?
Why does…

RétroX
- 1,996
- 4
- 16
- 24
45
votes
3 answers
Is constexpr-based computation Turing complete?
We know that C++ template metaprogramming is Turing complete, but preprocessor metaprogramming is not.
C++11 gives us a new form of metaprogramming: computation of constexpr functions. Is this form of computation Turing-complete? I am thinking that…

HighCommander4
- 50,428
- 24
- 122
- 194
45
votes
1 answer
GCC accepts `constexpr struct {} s;` but Clang rejects it. Who is correct?
The following code compiles fine with GCC:
constexpr struct {} s;
But Clang rejects it with the following error:
error: default initialization of an object of const type 'const struct (anonymous struct at …)' without a user-provided default…

HolyBlackCat
- 78,603
- 9
- 131
- 207
45
votes
4 answers
Does constexpr imply noexcept?
Does constexpr specifier imply noexcept specifier for a function? Answer to the similar question says "yes" concerning inline specifier, but Eric Niebler's article makes me wonder about possible answer to the current one. On my mind answer can…

Tomilov Anatoliy
- 15,657
- 10
- 64
- 169
45
votes
3 answers
Getting around the reinterpret cast limitation with constexpr
In c++11, a constexpr expression cannot contain reinterpret casts. So for instance, if one wanted to manipulate the bits in a floating point number, say to find the mantissa of the number:
constexpr unsigned int mantissa(float x) {
return…

nbubis
- 2,304
- 5
- 31
- 46
43
votes
8 answers
C++11: Compile Time Calculation of Array
Suppose I have some constexpr function f:
constexpr int f(int x) { ... }
And I have some const int N known at compile time:
Either
#define N ...;
or
const int N = ...;
as needed by your answer.
I want to have an int array X:
int X[N] = { f(0),…

Andrew Tomazos
- 66,139
- 40
- 186
- 319
42
votes
1 answer
"constexpr if" vs "if" with optimizations - why is "constexpr" needed?
C++1z will introduce "constexpr if" - an if that will have one of branches removed, based on the condition. Seems reasonable and useful.
However, is it not possible to do without constexpr keyword? I think that during compilation, compiler should…

MateuszL
- 2,751
- 25
- 38
42
votes
6 answers
Why isn't a for-loop a compile-time expression?
If I want to do something like iterate over a tuple, I have to resort to crazy template metaprogramming and template helper specializations. For example, the following program won't work:
#include
#include
#include…

user6416815
- 449
- 1
- 4
- 3
42
votes
3 answers
Can I obtain C++ type names in a constexpr way?
I would like to use the name of a type at compile time. For example, suppose I've written:
constexpr size_t my_strlen(const char* s)
{
const char* cp = s;
while(*cp != '\0') { cp++; };
return cp - s;
}
and now I want to…

einpoklum
- 118,144
- 57
- 340
- 684
41
votes
1 answer
C++ compile time counters, revisited
TL;DR
Before you attempt to read this whole post, know that:
a solution to the presented issue has been found by myself, but I'm still eager to know if the analysis is correct;
I've packaged the solution into a fameta::counter class that solves a…

Fabio A.
- 2,517
- 26
- 35
41
votes
1 answer
Why can't decomposition declarations be constexpr?
Consider the following snippet to test the upcoming C++17 feature decomposition declarations (formerly known as structured bindings)
#include
#include
constexpr auto divmod(int n, int d)
{
return std::make_pair(n / d, n %…

TemplateRex
- 69,038
- 19
- 164
- 304
41
votes
1 answer
`static constexpr` function called in a constant expression is...an error?
I have the following code:
class MyClass
{
static constexpr bool foo() { return true; }
void bar() noexcept(foo()) { }
};
I would expect that since foo() is a static constexpr function, and since it's defined before bar is declared, this…

Kyle Strand
- 15,941
- 8
- 72
- 167
40
votes
2 answers
constexpr exp, log, pow
I'd like to use constexpr versions of standard functions like exp, log, pow in a portable way. I currently have a non-portable solution g++ treats these functions as constexpr - a non-compliant extension of C++, but I am concerned about…

user1476176
- 1,045
- 1
- 7
- 15
40
votes
6 answers
Why can't a destructor be marked constexpr?
In C++, you can declare many things as constexpr: variables, functions (including member functions and operators), constructors, and since C++1z, also if statements and lambda expressions. However, declaring a destructor constexpr results in an…

s3rvac
- 9,301
- 9
- 46
- 74