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
57
votes
6 answers

C++ Linker Error With Class static constexpr

I am compiling the following simple program with g++-4.6.1 --std=c++0x: #include struct S { static constexpr int X = 10; }; int main() { return std::min(S::X, 0); }; I get the following linker error: /tmp/ccBj7UBt.o: In…
Travis Gockel
  • 26,877
  • 14
  • 89
  • 116
57
votes
1 answer

Why can't non-static data members be constexpr?

This is valid code: struct S { constexpr S(int x, int y): xVal(x), yVal(y) {} constexpr S(int x): xVal(x) {} constexpr S() {} const int xVal { 0 }; const int yVal { 0 }; }; But here I'd really like to declare xVal and yVal…
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
57
votes
3 answers

When and why would you use static with constexpr?

As a disclaimer, I have done my research on this before asking. I found a similar SO question but the answer there feels a bit "strawman" and didn't really answer the question for me personally. I've also referred to my handy cppreference page but…
void.pointer
  • 24,859
  • 31
  • 132
  • 243
56
votes
6 answers

In C++11 is sqrt defined as constexpr?

In C++11 is std::sqrt defined as constexpr, i.e. can it legally be used from other constexpr functions or in compile-time contexts like array sizes or template arguments? g++ seems to allow it (using -std=c++0x), but I'm not sure I can take that as…
sepp2k
  • 363,768
  • 54
  • 674
  • 675
55
votes
4 answers

Why do we need to mark functions as constexpr?

C++11 allows functions declared with the constexpr specifier to be used in constant expressions such as template arguments. There are stringent requirements about what is allowed to be constexpr; essentially such a function encapsulates only one…
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
54
votes
5 answers

Why is C++11 constexpr so restrictive?

As you probably know, C++11 introduces the constexpr keyword. C++11 introduced the keyword constexpr, which allows the user to guarantee that a function or object constructor is a compile-time constant. [...] This allows the compiler to…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
54
votes
4 answers

How does the C++ compiler evaluate recursive constexpr functions so quickly?

I've been learning about C++ constexpr functions, and I implemented a constexpr recursive function to find the nth fibonacci number. #include #include #include #include #include constexpr long long…
ahskdjfk
  • 919
  • 5
  • 8
54
votes
4 answers

Use of constexpr in header file

Can I have a definition like this in a header file? constexpr double PI=3.14; Is there any problem in having this in a header file that would be included to several cpp files? I am worried that since it says in standard that this constexpr has its…
mans
  • 17,104
  • 45
  • 172
  • 321
52
votes
4 answers

Can't a class have static constexpr member instances of itself?

This code is giving me incomplete type error. What is the problem? Isn't allowed for a class to have static member instances of itself? Is there a way to achieve the same result? struct Size { const unsigned int width; const unsigned int…
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
52
votes
3 answers

Why do constant expressions have an exclusion for undefined behavior?

I was researching what is allowed in a core constant expression*, which is covered in section 5.19 Constant expressions paragraph 2 of the draft C++ standard which says: A conditional-expression is a core constant expression unless it involves one…
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
51
votes
3 answers

Does specifying constexpr on a constructor automatically make all objects created from it to be constexpr?

Here is my code: class test { public: constexpr test() { } constexpr int operator+(const test& rhs) { return 1; } }; int main() { test t; //constexpr keyword isn't…
Carlos Miguel Colanta
  • 2,685
  • 3
  • 31
  • 49
48
votes
3 answers

How can the compile-time be (exponentially) faster than run-time?

The below code calculates Fibonacci numbers by an exponentially slow algorithm: #include #include #define DEBUG(var) { std::cout << #var << ": " << (var) << std::endl; } constexpr auto fib(const size_t n) -> long long { …
behzad.nouri
  • 74,723
  • 18
  • 126
  • 124
48
votes
5 answers

static constexpr member of same type as class being defined

I would like a class C to have a static constexpr member of type C. Is this possible in C++11? Attempt 1: struct Foo { constexpr Foo() {} static constexpr Foo f = Foo(); }; constexpr Foo Foo::f; g++ 4.7.0 says: 'invalid use of incomplete…
ndkrempel
  • 1,906
  • 14
  • 18
47
votes
3 answers

How can I get the depth of a multidimensional std::vector at compile time?

I have a function that takes a multidimensional std::vector and requires the depth (or the number of dimensions) to be passed in as a template parameter. Instead of hardcoding this value I would like to write a constexpr function that will take the…
tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
47
votes
1 answer

Get min / max value of a static constexpr array at compile time

Let's say I have an array of integers defined like that: static constexpr int IntArray[] = {1, 5, 10, 12, 17}; Is there a way to get the minimum or maximum value at compile time?
lucaboni
  • 2,334
  • 2
  • 29
  • 41