Questions tagged [constexpr-function]
42 questions
8
votes
1 answer
constexpr result from non-constexpr call
Recently I was surprised that the following code compiles in clang, gcc and msvc too (at least with their current versions).
struct A {
static const int value = 42;
};
constexpr int f(A a) { return a.value; }
void g() {
A a; //…

dtldarek
- 949
- 1
- 13
- 17
6
votes
3 answers
A constexpr function that calculates how deep a std::vector is nested
Is there a way to write a constexpr function that returns how deep a std::vector is nested?
Example:
get_vector_nested_layer_count>>() // 2
get_vector_nested_layer_count>>>() //…

palapapa
- 573
- 2
- 5
- 25
5
votes
1 answer
What is the use of a constexpr function in C++23?
The keyword constexpr enforced pretty tight restrictions on functions on its introduction into the C++11 standard. These restrictions were loosened with C++14 and C++20 (most noteworthy):
C++14 allowed multiple return statements, static_asserts…

Brotcrunsher
- 1,964
- 10
- 32
5
votes
1 answer
Is it possible with C++20 to have a constexpr function return a tuple of types that have static constexpr array's with a value passed though a macro?
After two or three days of trying, I had to give up and wrote a "minimal" test case I hope demonstrates the problem.
What I need is a method to convert string-literals, that are passed as macro arguments without quotes, into strings (catenated with…

Carlo Wood
- 5,648
- 2
- 35
- 47
5
votes
1 answer
A "constexpr" function should not be declared "inline"
By analyzing code using SonarLint, I got a message (the title of the question) about a destructor that is declared like below:
class Foo
{
public:
. // default ctor
. // parameterized ctor
.
inline ~Foo() = default; // dtor
.
. // copy…

digito_evo
- 3,216
- 2
- 14
- 42
5
votes
1 answer
The Requirement for "Literal Type" in "constexpr" Functions
Here is my code:
class agg_t1{
int x; // private non-static data menber
};
class agg_t2{
agg_t2(){} // user-provided constructor
};
constexpr void ce1(agg_t1 arg){}; // OK
constexpr void ce2(agg_t2 arg){}; // ERROR: …

absuu
- 340
- 1
- 11
5
votes
0 answers
constexpr function only works if declared as a seemingly irrelevant template
The following code doesn't compile; g++ 7.3.0 with --std=c++17 gives the error message
invalid return type 'const C' of constexpr function 'constexpr const C operator+(const C&, int)'
note: 'C' is not literal because 'C' has a non-trivial…

TonyK
- 16,761
- 4
- 37
- 72
5
votes
1 answer
constexpr constructor's parameter type 'std::function' is not a literal type
I'm writting a simple C++ HTTP server framework. In my Server class, I can add Route's. Every route consists of a path, an HTTP method and a Controller (which is a pipeline of functions to be called when the request was made.) That Controller class…

André Winston
- 167
- 5
4
votes
1 answer
Why does gcc 12.2 not optimise divisions into shifts in this constexpr function called from main()
I have been playing around with the Godbolt Compiler and typed this code:
constexpr int func(int x)
{
return x > 3 ? x * 2 : (x < -4 ? x - 4 : x / 2);
}
int main(int argc)
{
return func(argc);
}
The code is somewhat straight forward. The…

Finn Eggers
- 857
- 8
- 21
4
votes
3 answers
Compiler can't execute constexpr expression
I have code something like this:
template
constexpr size_t get_init_size(Args ... args) {
return sizeof...(Args);
}
template
constexpr auto make_generic_header(Args ... args) {
constexpr size_t…

emik_g
- 109
- 5
3
votes
1 answer
What are the conditions that `constexpr` will start a new constant expression context?
I asked a question in Why `constexpr` specifier is not allowed for non-empty `std::vector`? previously, after finding the answer, I have another question here.
I tested and found that it is valid to dynamically allocate memory in one constexpr…

Waker
- 216
- 9
3
votes
1 answer
virtual and constexpr before up to C++17
I'm confused about what I read about mixing virtual and constexpr for member functions.
According to:
constexpr and virtual
Can virtual functions be constexpr?
constexpr in cppreference
up to C++17 included, it should not be possible to mix…

Oersted
- 769
- 16
3
votes
0 answers
Can `constexpr` functions contain side-effects?
I'm working through 'Programming: Principles and Practice Using C' and need some help understanding this bit on constexpr functions on page 291:
A constexpr function behaves just like an ordinary function until you use it where a constant is…

user51462
- 1,658
- 2
- 13
- 41
3
votes
1 answer
Why should constexpr functions contain exactly one return statement?
Reading through the constexpr specifier documentation on cppreference, I've noticed that the standard says the following:
[...] the function body [of constexpr function] must be either deleted or defaulted or contain only the following: [...] if…

jsfpdn
- 73
- 1
- 6
3
votes
2 answers
constexpr function which is shared in multiple modules
I noticed a strange behavior when I was working with a constexpr function.
I reduced the code to a simplified example.
Two functions are called from two different translation units (module A and B).
#include
int mod_a();
int…

MiCo
- 399
- 2
- 6