Questions tagged [constexpr-function]
42 questions
3
votes
3 answers
Why constexpr method can return correctly class members whose value change during execution?
I just discovered that a constexpr method can return correctly the value of a class member that changes during execution. My question is, how is this possible if constexpr methods are supposed to be evaluated completely at compile time?
The example…

Victor
- 460
- 3
- 11
3
votes
1 answer
What does [decl.constexpr].5 mean exactly?
The standard on constexpr functions states under point 5 of [decl.constexpr]:
For a non-template, non-defaulted constexpr function or a non-template, non-defaulted, non-inheriting constexpr constructor, if no argument values exist such that an…

bitmask
- 32,434
- 14
- 99
- 159
2
votes
1 answer
Different compilation results with template and non-template class having constexpr constructor
A constructor fails to quality as constexpr if the class contains std::vector as its data member since std::vector doesn't have constexpr constructor (atleast until C++17 standards, for C++20 I came across this draft).
The issue is if the class…

Satyam Sharma
- 29
- 5
2
votes
3 answers
error while trying to compile .data() from std::array as a constexpr function in c++20
I was trying to compute an array in compilation time to speed up some functions while i encountered an error which I was unable to resolve with the help of cppreference.
The code boils down to this:
#include
#include…

jabluko
- 23
- 3
2
votes
2 answers
What is the proper definition for a constexpr function that take a character array?
I'm writing a hashing function to help speed up string comparisons.
My codebase compares strings against a lot of const char[] constants, and it would be ideal if I could work with hashes instead. I went ahead and translated xxHash to modern C++,…

Andi Abrudan
- 104
- 4
2
votes
2 answers
how to check that const array members grow monotonically at compile time
assume we have const array:
const int g_Values[] = { ... };
how check that members grow monotonically at compile time, i.e. g_Values[i] < g_Values[i + 1]
in runtime this possible to check like this:
bool IsMonotonously()
{
int i =…

lomo
- 23
- 3
2
votes
0 answers
X is not a valid template argument for 'const char*' because it is not the address of a variable
I'm trying to use the resources of a temporary class object as a template parameter. But apparently this doesn't work:
godbolt
#include
constexpr size_t size(const char* s)
{
int i = 0;
while(*s!=0) {
++i;
++s;
…

glades
- 3,778
- 1
- 12
- 34
2
votes
1 answer
Why does full `constexpr` enabling of a data structure cause the compiled code to be bigger?
At this moment of Jason Turner's 2016 CppCon talk "Practical Performance Practices", he mentions that full constexpr enabling of every data structure that can be (I'm guessing that means making every field and function constexpr that can be) can…

bun9
- 161
- 8
2
votes
1 answer
Constexpr constants inside a constexpr function?
Suppose I have a static function which takes an enum and returns a cstring ptr for debugging.
The function can be constexpr but no guarantee is made that it can always be evaluated at compile time. Say it is running on a microcontroller and this is…

GreenaGiant
- 73
- 9
2
votes
1 answer
constexpr constructor not called as constexpr for implicit type conversion
I made some code which is capable of dispatching to a function based upon the call-site providing a string associated with a given function (via a tuple of function pointers and a parallel array). Instead of accepting a string directly, the…

Sean
- 393
- 2
- 11
1
vote
2 answers
Static assertion if one std::array is a subset of the other
I'm using two constexpr std::array:
constexpr std::array full = { 1,2,3 };
constexpr std::array subset = { 3 };
I would like to static assert if the second is a subset of the first.
In the above example, the assertion should succeed, and in the…

AK87
- 613
- 6
- 24
1
vote
0 answers
Getting the value of a constexpr std::variant via constexpr function
This is a reduced example of a bigger function. The core issue is that at one point I try to get the value out of a constexpr std::variant via a constexpr/consteval function. This fails and I do not know why.
Code:
#include
#include…

std_unordered_map
- 113
- 6
1
vote
1 answer
Implementation of typed tuple wrapper
How can an implementation look like, that wraps around e.g. a std::tuple as a static list of type/value, plus a type (not contained in tuple) to refer to some kind of owner/visitor.
I want to instantiate like
constexpr auto data = data_of(1,…

Chris G.
- 816
- 2
- 15
1
vote
1 answer
Why doesn't constexpr function returning std::string doesn't compile when outside templated class?
Note: I am using gcc, but tested on godbolt.org and it also works on msvc, but not on clang
I accidentally discovered that the following simple function compiles while being in a templated class, but not as a free function. Could someone explain…

Radu C
- 303
- 2
- 11
1
vote
1 answer
Concatenate string literals at compile time
I need to concatenate a variable number of string literals into one to use it in static_assert()
I tried using templates with structs, but compiler does not like literals as template parameters.
error: the address of ‘m1’ is not a valid template…

DiKetarogg
- 170
- 8