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
2
votes
0 answers
constexpr modifier has no effect
I have some problem with constexpr functions.
I thought, any function declared with constexpr modifier must return compile-time constant, but actually it doesn't work. (Eclipse + MinGW, gcc 4.8.1)
#include
#include…

alphashooter
- 304
- 1
- 7
2
votes
3 answers
Switch case statement with member variable in case
I am trying to find a way to evaluate a switch-case statement using a member variable in the case part. I thought that having a global static variable like below would be allowed as const-expression.
Unfortunately the compiler tells me the…

Abruzzo Forte e Gentile
- 14,423
- 28
- 99
- 173
2
votes
1 answer
constexpr won't work using Visual C++ Compiler Nov 2013 CTP (CTP_Nov2013)
I'm trying to implement a compile-time hash algorithm using constexpr. I Downloaded Nov 2013 CTP because it has supported for constexpr but thats a lie...
#define hashCharacter(T, J) (((T >> 0x0D) | (T << 0x13)) + J)
unsigned long constexpr…

Agustin Alvarez
- 349
- 1
- 2
- 11
2
votes
1 answer
constexpr constructor with compile time validation
I'd like to build up a class with the option of constexpr-ness. And, of course, I'd like to take advantage of compile time error check.
Every constexpr function, constructor included, must work also at runtime, when the given parameters are not…

nyarlathotep108
- 5,275
- 2
- 26
- 64
2
votes
1 answer
link error while compiling this "simple" program with clang and g++
I am working on a project, and it seems that clang is unable to generate a valid bytecode (as the linker fail to link, some static constexpr in a template class is not found at link time)
I can fix it with a static getter in the class, but this lead…

neam
- 894
- 9
- 19
2
votes
2 answers
constexpression subscript operator of STL containers
constexpr const_reference at( size_type pos ) const;
How can this overload of STL container accessors work with non-constexpr parameters? What are classical use cases of this overload?

Meteorhead
- 480
- 3
- 13
2
votes
3 answers
const double expression in C++
in the English Wikipedia page on C++11, we can read that:
Prior to C++11, the values of variables could be used in constant expressions only if the variables are declared const, have an initializer which is a constant expression, and are of…

Greg82
- 1,000
- 3
- 10
- 24
2
votes
1 answer
profiling constexpr with const char array parameters shows run-time execution
I do a lot of hashing in a program of mine, so I decided to hack up a constexpr function that can do at least some of the hashing for me at compile-time. After successfully implementing a constexpr hash function, I profiled the code and it is…

Brandon
- 483
- 3
- 12
2
votes
3 answers
C++ Declare array whose size value from const array
I'm trying to define a stack c-style array whose size is taken from const array and is known in compile-time.
const int size[2]={100, 100};
int ar[size[0]]; //error: expression must have a constant value
It fails. How it can be fixed?

Mark Kahn
- 1,130
- 10
- 8
2
votes
0 answers
how can I create a template from string literal?
I want to create a template class from string literal:
create_eq("abcdefg") get IEqual<'a','b','c','d','e'>
but ISO C++11's User-defined literals only support integer-literal and floating-literal :
template
IEqual …

fe263
- 215
- 1
- 7
2
votes
1 answer
constexpr-izing the jesteress hashing algorithm
After some head-scratching, I've managed to constexpr-ize the Jesteress hashing algorithm. The compiler, however, refuses to generate a constant out of a cjesteress() call, such as in std::cout << cjesteress("test) << std::endl;, but generates code…

user1095108
- 14,119
- 9
- 58
- 116
2
votes
2 answers
Why using constant expression as a template parameter?
When it is better to have a private data member (_val in class B) and when it is better to have val as a template parameter (class A)?
#include
using namespace std;
template
class A{
public:
A(){ cout << val <<…

cpp
- 3,743
- 3
- 24
- 38
2
votes
1 answer
Compiler error when using constexpr and lambda
I encountered a problem when using constexpr functions together with lambdas.
The following code is a minimal version which reproduces the error:
#include
constexpr unsigned bar(unsigned q) {
return q;
}
template

Danvil
- 22,240
- 19
- 65
- 88
2
votes
1 answer
Variadic template doesn't recognise a constexpr function
I'm trying to initialize some C++ array at compile time but I got a weird g++ error. Here is the smallest chunk of code I've been able to get which reproduce the error:
#include
template
constexpr Ar Map(typename…

hivert
- 10,579
- 3
- 31
- 56
2
votes
1 answer
Literals and constexpr functions, compile-time evaluation
Attempting to implement a pleasing (simple, straightforward, no TMP, no macros, no unreadable convoluted code, no weird syntax when using it) compile-time hash via user-defined literals, I found that apparently GCC's understanding of what's a…

Damon
- 67,688
- 20
- 135
- 185