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
81
votes
9 answers
Create N-element constexpr array in C++11
Hello i'm learning C++11, I'm wondering how to make a constexpr 0 to n array, for example:
n = 5;
int array[] = {0 ... n};
so array may be {0, 1, 2, 3, 4, 5}

Christopher Aldama
- 821
- 1
- 7
- 5
80
votes
1 answer
What's the difference between static constexpr and static inline variables in C++17?
With C++17 we get inline variables.
One of the uses for them is to define constant fields in classes.
So what's the difference between these two constant definitions:
class MyClass {
static constexpr int myFirstVar = 10;
static const inline…

fen
- 9,835
- 5
- 34
- 57
78
votes
6 answers
enum vs constexpr for actual static constants inside classes
Let me start by stating my intent. In the olden (C++) days, we would have code like:
class C
{
public:
enum {SOME_VALUE=27};
};
Then we could use SOME_VALUE throughout our code as a compile time constant and wherever the compiler would see…

Michael Goldshteyn
- 71,784
- 24
- 131
- 181
72
votes
2 answers
When does a constexpr function get evaluated at compile time?
Since it is possible that a function declared as constexpr can be called during run-time, under which criteria does the compiler decide whether to compute it at compile-time or during runtime?
template
constexpr…

Byzantian
- 3,208
- 4
- 27
- 31
69
votes
4 answers
Is constexpr supported with lambda functions / expressions?
struct Test
{
static const int value = []() -> int { return 0; } ();
};
With gcc-4.6 I get something like, error: function needs to be constexpr. I have tried multiple combinations of putting constexpr at various places, but no luck.
Is constexpr…

iammilind
- 68,093
- 33
- 169
- 336
69
votes
3 answers
Equivalent ternary operator for constexpr if?
Maybe I missed something, but I can't find any hints: is there a constexpr ternary operator in C++17 equivalent to constexpr-if?
template
class BusAddress {
public:
explicit constexpr BusAddress(Address device) :
…

wimalopaan
- 4,838
- 1
- 21
- 39
69
votes
7 answers
constexpr if and static_assert
P0292R1 constexpr if has been included, on track for C++17. It seems useful (and can replace use of SFINAE), but a comment regarding static_assert being ill-formed, no diagnostic required in the false branch scares me:
Disarming static_assert…

Johan Lundberg
- 26,184
- 12
- 71
- 97
69
votes
3 answers
constexpr const vs constexpr variables?
It seems obvious that constexpr implies const and thus it is common to see:
constexpr int foo = 42; // no const here
However if you write:
constexpr char *const str = "foo";
Then GCC will spawn "warning: deprecated conversion from string constant…

Thomas Moulard
- 5,151
- 2
- 21
- 28
69
votes
3 answers
Constexpr Math Functions
So noticed from this page that none of the math functions in c++11 seems to make use of constexpr, whereas I believe all of them could be. So that leaves me with two questions, one is why did they choose not to make the functions constexpr. And two…

aaronman
- 18,343
- 7
- 63
- 78
67
votes
2 answers
When would I use std::integral_constant over constexpr?
#include
#include
int main(){
//creating an integral constant with constexpr
constexpr unsigned int speed_of_light{299792458};
//creating an integral constant with std::integral_constant
typedef…

Trevor Hickey
- 36,288
- 32
- 162
- 271
66
votes
2 answers
Why isn't `std::initializer_list` defined as a literal type?
This is a follow-up of this question: Is it legal to declare a constexpr initializer_list object?.
Since C++14, the std::initializer_list class has all of its methods marked with constexpr. It seems natural to be able to initialize an instance by…

ChristopherC
- 1,635
- 16
- 31
65
votes
3 answers
Whyever **not** declare a function to be `constexpr`?
Any function that consists of a return statement only could be declared
constexpr and thus will allow to be evaluated at compile time if all
arguments are constexpr and only constexpr functions are called in its body. Is there any reason not to…

Lars
- 2,616
- 3
- 21
- 18
61
votes
2 answers
C++11 - static_assert within constexpr function?
How would one properly do a static_assert within a constexpr function? For example:
constexpr int do_something(int x)
{
static_assert(x > 0, "x must be > 0");
return x + 5;
}
This is not valid C++11 code, because a constexpr function must only…

RétroX
- 1,996
- 4
- 16
- 24
58
votes
1 answer
static constexpr variable vs function
Is there a difference between declaring floating point constant as a static constexpr variable and a function as in example below, or is it just a matter of style?
class MY_PI
{
public:
static constexpr float MY_PI_VAR = 3.14f;
static…

user2052436
- 4,321
- 1
- 25
- 46
57
votes
9 answers
constexpr overloading
Related: Function returning constexpr does not compile
I feel like constexpr is limited in usefulness in C++11 because of the inability to define two functions that would otherwise have the same signature, but have one be constexpr and the other not…

David Stone
- 26,872
- 14
- 68
- 84