Questions tagged [constexpr]

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.

2435 questions
822
votes
10 answers

What's the difference between constexpr and const?

What's the difference between constexpr and const? When can I use only one of them? When can I use both and how should I choose one?
MBZ
  • 26,084
  • 47
  • 114
  • 191
458
votes
6 answers

const vs constexpr on variables

Is there a difference between the following definitions? const double PI = 3.141592653589793; constexpr double PI = 3.141592653589793; If not, which style is preferred in C++11?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
389
votes
15 answers

When should you use constexpr capability in C++11?

It seems to me that having a "function that always returns 5" is breaking or diluting the meaning of "calling a function". There must be a reason, or a need for this capability or it wouldn't be in C++11. Why is it there? // preprocessor. #define…
Warren P
  • 65,725
  • 40
  • 181
  • 316
293
votes
5 answers

Is it possible to use std::string in a constexpr?

Using C++11, Ubuntu 14.04, GCC default toolchain. This code fails: constexpr std::string constString = "constString"; error: the type ‘const string {aka const std::basic_string}’ of constexpr variable ‘constString’ is not literal... because... …
Vector
  • 10,879
  • 12
  • 61
  • 101
281
votes
3 answers

Does static constexpr variable inside a function make sense?

If I have a variable inside a function (say, a large array), does it make sense to declare it both static and constexpr? constexpr guarantees that the array is created at compile time, so would the static be useless? void f() { static constexpr…
David Stone
  • 26,872
  • 14
  • 68
  • 84
234
votes
6 answers

Undefined reference to static constexpr char[]

I want to have a static const char array in my class. GCC complained and told me I should use constexpr, although now it's telling me it's an undefined reference. If I make the array a non-member then it compiles. What is going on? // .hpp struct…
Pubby
  • 51,882
  • 13
  • 139
  • 180
146
votes
2 answers

Does constexpr imply inline?

Consider the following inlined function : // Inline specifier version #include #include inline int f(const int x); inline int f(const int x) { return 2*x; } int main(int argc, char* argv[]) { return…
Vincent
  • 57,703
  • 61
  • 205
  • 388
138
votes
3 answers

Constexpr vs macros

Where should I prefer using macros and where should I prefer constexpr? Aren't they basically the same? #define MAX_HEIGHT 720 vs constexpr unsigned int max_height = 720;
Tom Dorone
  • 1,575
  • 2
  • 9
  • 7
103
votes
8 answers

Computing length of a C string at compile time. Is this really a constexpr?

I'm trying to compute the length of a string literal at compile time. To do so I'm using following code: #include int constexpr length(const char* str) { return *str ? 1 + length(str + 1) : 0; } int main() { printf("%d %d",…
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
100
votes
2 answers

Difference between "if constexpr()" Vs "if()"

What is the difference between if constexpr() and if()? Where and When can I use both of them?
msc
  • 33,420
  • 29
  • 119
  • 214
96
votes
2 answers

What does it mean to "poison a function" in C++?

At the very end of Scott Schurr's talk "Introducing constexpr" at CppCon, he asks "Is there a way to poison a function"? He then explains that this can be done (albeit in a non-standard way) by: Putting a throw in a constexpr function Declaring an…
sudo make install
  • 5,629
  • 3
  • 36
  • 48
91
votes
2 answers

Is it possible to declare constexpr class in a header and define it in a separate .cpp file?

I have a class Dimension which I defined (like all my classes) in a file Dimension.h: class Dimension { public: constexpr Dimension() noexcept; constexpr Dimension(int w, int h) noexcept; int width; int height; }; I thought I…
Timo Türschmann
  • 4,388
  • 1
  • 22
  • 30
90
votes
2 answers

constexpr vs. static const: Which one to prefer?

For defining compile-time constants of integral types like the following (at function and class scope), which syntax is best? static const int kMagic = 64; // (1) constexpr int kMagic = 64; // (2) (1) works also for C++98/03 compilers, instead…
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
89
votes
7 answers

How to declare constexpr extern?

Is it possible to declare a variable extern constexpr and define it in another file? I tried it but the compiler gives error: Declaration of constexpr variable 'i' is not a definition in .h: extern constexpr int i; in .cpp: constexpr int i = 10;…
coldbrew
  • 903
  • 1
  • 6
  • 5
87
votes
3 answers

Will consteval functions allow template parameters dependent on function arguments?

In C++17, this code is illegal: constexpr int foo(int i) { return std::integral_constant::value; } That's because even if foo can be evaluated at compile-time, the compiler still needs to produce the instructions to execute it at…
Annyo
  • 1,387
  • 9
  • 20
1
2 3
99 100