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.

1942 questions
682
votes
10 answers

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
  • 22,515
  • 41
  • 105
  • 182
355
votes
14 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
  • 58,696
  • 38
  • 168
  • 301
352
votes
4 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
  • 237,063
  • 85
  • 359
  • 638
216
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
  • 22,053
  • 14
  • 61
  • 77
207
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
  • 48,511
  • 12
  • 121
  • 172
204
votes
4 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
  • 8,941
  • 10
  • 52
  • 96
122
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
  • 50,257
  • 51
  • 171
  • 339
104
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,209
  • 2
  • 7
  • 6
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,341
  • 3
  • 26
  • 45
95
votes
6 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
  • 18,498
  • 26
  • 109
  • 202
74
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,201
  • 5
  • 17
70
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
  • 3,498
  • 1
  • 21
  • 28
70
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
  • 65,547
  • 23
  • 122
  • 176
67
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
  • 8,812
  • 5
  • 29
  • 53
67
votes
7 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
  • 681
  • 1
  • 6
  • 5
1
2 3
99 100