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]
1942 questions
1
vote
2 answers
constexpr reference to avr port address
I am playing a bit with avr microcontrollers and C++14. While trying to implement a C++ wrapper for the io pins I stumbled upon an error.
The idea was, to let the wrapper take the sfrs as template argument to easy optimaizion for compiler (don't…
![](../../users/profiles/1857229.webp)
Uroc327
- 1,264
- 1
- 10
- 22
1
vote
2 answers
Constexpr and know how count class
I'm working on an Entity Component System and trying to make the Component type number based on how many class are derived from the Component class itself.
But I think there is some missing features in C++ for doing all I want.
Because the number of…
![](../../users/profiles/5890258.webp)
Mathieu Van Nevel
- 1,352
- 1
- 8
- 23
1
vote
0 answers
Template error C2244 unable to match function definition to an existing declaration using constexpr in Microsoft Visual Studio 2015
I'm the getting : error C2244 unable to match function definition to an existing declaration while using constexpr.
template constexpr int x() {
return N*N;
}
template class B {
public:
int data;
};
template…
![](../../users/profiles/3919133.webp)
muzdima
- 23
- 1
- 4
1
vote
0 answers
C++ singleton wrapper for a STL container
I am working in a C++11 solution, for Linux (Ubuntu 14.04; then I should need to migrate it to REL). I am using g++4.8 compiler (and I should probably be required to compile using Intel compiler).
But, please, focus on C++11 / Ubuntu 14.04 /…
![](../../users/profiles/6395577.webp)
Karol Baum
- 63
- 7
1
vote
1 answer
Non-type template arguments explicit instantiation in source file
I am wrapping a library into my class design. I would like to call a template method with unsigned int non-type parameter provided in the library in my class constructor.
#include
#include // template header in here
class foo {
…
![](../../users/profiles/5231637.webp)
yc2986
- 993
- 2
- 17
- 22
1
vote
0 answers
Is there a way to define the value of a constexpr value when compiling?
When using macros, it's possible to define the value when compiling with -D =.
Can something similar to be done with constexpr values?
To motivate the problem, consider the following code which generates a 12x12 multiplication table at…
![](../../users/profiles/2883245.webp)
erip
- 13,935
- 9
- 51
- 102
1
vote
1 answer
Use of constexpr and functions
I'm trying to play around with constexpr and static_assert. I actually need to check the length of a constexpr string, which is calculated by a dedicated function. Here is what I'm trying to run :
#include
using namespace std;
class…
![](../../users/profiles/6912490.webp)
Antoine Morel
- 67
- 4
1
vote
1 answer
What is the function parameter equivalent of constexpr?
We are trying to speedup some code under Clang and Visual C++ (GCC and ICC is OK). We thought we could use constexpr to tell Clang a value is a compile time constant but its causing a compile error:
$ clang++ -g2 -O3 -std=c++11 test.cxx -o…
![](../../users/profiles/608639.webp)
jww
- 83,594
- 69
- 338
- 732
1
vote
2 answers
Assign multibyte ASCII literals to enum values
One can assign an ASCII literal (can't call it a string) to enum value as following:
#include
// Macro to handle BIG/LITTLE ENDIAN
// Endianness is suppoesed to handled in this macro
#define TEMP(X) X
enum t
{
XX = 'AA', // 0x4141 …
![](../../users/profiles/4373992.webp)
jha-G
- 1,944
- 14
- 29
1
vote
1 answer
How to make msvc 14 evaluate my constexpr at compile time
I am experimenting with c++ constexpr. I am implementing a HashedString class using FNV-1a hash.
Everything seems fine except that visual studio 2015 update 3 doesn't seem to evaluate the constexpr at compile time.
I added a static_assert and it…
![](../../users/profiles/2576606.webp)
user2576606
- 35
- 4
1
vote
0 answers
Error when declaring static constexpr instance of derived class in CRTP base class
I'm building a container type and attempting to reuse as much code as possible using the Curiously Recurring Template Pattern.
Here's the base code, which compiles in some cases, but not in others:
template
![](../../users/profiles/232593.webp)
Merlyn Morgan-Graham
- 54,918
- 14
- 119
- 174
1
vote
2 answers
Why I can't increment the parameter of the simple constexpr function?
Visual Studio 2015 Update 3.
I read the Programming. Principles and Practice Using C++ (second edition) by Bjarne Stroustrup. I learn the constexpr functions...
It works:
constexpr int get_value(int n) {
return n + 1;
}
But I can't compile this…
![](../../users/profiles/1306132.webp)
Andrey Bushman
- 10,107
- 12
- 64
- 150
1
vote
1 answer
Terminating template search
Suppose I have a template class like so:
template
struct NullTArrayLength{
static constexpr size_t value = NullTArrayLength::value;
};
/* Some how have a specialization that terminates on when array[0] =…
![](../../users/profiles/6149078.webp)
DarthRubik
- 3,657
- 14
- 49
1
vote
1 answer
Take the length of a string implicitly in templates
Ok, suppose I have this magic template that takes a char array and turns it into a template parameter list. For example: "thing" is translated to list<'t','h','i','n','g'>:
template
ArrayToList;
You use it like this:
constexpr…
![](../../users/profiles/6149078.webp)
DarthRubik
- 3,657
- 14
- 49
1
vote
1 answer
GCC and clang: subexpression not valid in a constant expression
Consider the following code:
template
static constexpr int f(int v) {
int a[] = { (v ^= V, 0)... }; // Line 3
return v;
}
static constexpr int i = f<0x00>(0x11); // Line 7
int main() { }
It compiles with GCC and fails to…
![](../../users/profiles/4987285.webp)
skypjack
- 45,296
- 16
- 80
- 161