61 questions
2
votes
2
answers
2k
views
How can I initialise a constexpr array with values using std::generate
For example, if I wanted a constexpr std::array<int,100> initialised with all the multiples of 3 from 1-300 at compile time how can I do this?
My first thought was to use std::generate, ...
2
votes
1
answer
101
views
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. ...
5
votes
0
answers
95
views
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' ...
5
votes
1
answer
1k
views
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 ...
2
votes
1
answer
460
views
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). ...
0
votes
1
answer
2k
views
constexpr string-literal checking: short syntax, no runtime possibility
EDIT: Renamed, as my final solution does not use a poisoning method.
I'm looking for a way to prevent a constexpr method from being called at runtime. I'm writing a function that accepts a string ...
3
votes
1
answer
125
views
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 ...
-1
votes
2
answers
101
views
Do constexpr functions have to be defined before they are used? [duplicate]
see code below, f() is defined below main function is regarded as ill-formed ?
could anyone give me an explanation for this ?
constexpr int f ();
void indirection ();
int main () {
constexpr int n ...
31
votes
2
answers
4k
views
Use of constexpr function before definition fails
I'm having some trouble with constexpr. The book C++ Primer shows a line of code:
constexpr int sz = size(); // only size() is a constexpr function
// this code is right
...
26
votes
3
answers
38k
views
Can't use function parameter of a constexpr function in a constant expression
I have a constexpr function taking a single argument, and I'm calling the function with a constant. However, it seems I can't use that argument as a constant expression:
constexpr void f(const int i)
...
399
votes
15
answers
162k
views
Should I declare a constant instead of writing a constexpr function?
It seems to me that having a "function that always returns 5" is breaking or diluting the meaning of "calling a function". There must be a reason, or a need for this capability or ...