61 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
1
vote
1
answer
215
views
Making the non-trivial creation of an array constexpr
This is a follow-up of my answer to a question about initialization of arrays of non-default constructible types.
The core of the question can be summarized to this snippet, which is an over-...
6
votes
4
answers
286
views
Constexpr function that builds an integral value from a list of bits
Sometimes, it may be useful to build an integral value from a list of bits (in increasing order). Such a function could be named to_integral.
Example:
static_assert (to_integral(1,1,0,1,0,1) == ...
2
votes
1
answer
253
views
constexpr unsigned int * unsigned long not evaluated as unsigned long?
The following integer power function returns 0 when the result should be greater than 2^32 if the base argument is unsigned int, but works fine when the base argument type is changed to an unsigned ...
0
votes
1
answer
132
views
How to reconcile a constexpr function's argument being not constexpr?
A simple function like:
constexpr int f(int x) {
constexpr int y = x;
return y;
}
Seems to not be legal because x is not constexpr. This complaint is made even before I call the function, ...
4
votes
1
answer
239
views
using throw in a constexpr or a consteval function in order to generate compile-time error
The problem
Using static_assert to generate compile-time error is not always easy because it requires a constant expression as first argument. I found, on StackOverflow, several example where throw ...
3
votes
0
answers
63
views
constexpr if with function parameter error with clang, gcc is ok
Here is the test code:
#include <type_traits>
enum class ShaderType:unsigned{
Vertex = 0,
Fragment = 1,
};
template<ShaderType type> struct ShaderTypeHolder{
static constexpr ...
2
votes
2
answers
115
views
Stroustrup book constexpr example does not compile in VS C++ 2022
I am trying to reproduce constexpr example from Stroustrup book "The C++ Programming Language" 4th Ed, pp. 265-266. I am using Visual Studio 2022 Community. The code below does not compile, ...
0
votes
1
answer
317
views
Creating constexpr map filled by calls to a constexpr function in C++ 17
I have a constexpr function that calculates CRC at compile time. I need to create a map between CRCs that are generated by this function and string inputs and use them in runtime (Even better if it is ...
1
vote
3
answers
301
views
constexpr function that keeps multiplying a number until "it is big enough"
Ok, I completely messed up this question, first for the typo and second because my oversimplified minimum example didn't have any problem at all. I was considering to delete the question but seeing ...
2
votes
1
answer
299
views
How can I get a constexpr size from a std::set, which I can use to return a std::array with the number of elements in the std::set in C++23?
How can I get a constexpr size from a std::set, which I can use to return a std::array with the number of elements in the std::set in C++23 or to-be C++26, as far as supported by either G++ oder Clang?...
0
votes
1
answer
89
views
How do `constexpr` "compile time pointers" work in C++?
Background
Pointers cannot be used as T* p value parameters to a template (which is specialized at compile time), because the memory address &obj of some object T obj is only known at run time.
...
1
vote
2
answers
225
views
Using constexpr for both runtime and compile-time evaluations - passing constexpr value as a function parameter
I'm trying to make a constexpr function which will run both at compile and runtime depending on the call.The function will check a unsigned long and will return a number depending on it's value. ...
9
votes
2
answers
1k
views
Why is it possible to use the return of a lambda, passed as argument of a constexpr function argument, in a constant expression within the function?
The wording of the question title is probably incorrect and I'll fix it happily from your suggestions.
My question can be illustrated by this snippet:
#include <array>
template <typename ...
0
votes
1
answer
149
views
cout is able to run at compile-time [duplicate]
I think cout is only allowed to run at runtime but the following code proves otherwise. Or am I not understanding something deeper? I only expected it to print the message once, but it printed twice. ...
0
votes
1
answer
178
views
Where in the C++20 standard (N4860) does it say that an inline function must be defined before its use in a translation unit?
[dcl.constexpr]/1:
The constexpr specifier shall be applied only to the definition of a variable or variable template or the declaration of a function or function template. The consteval specifier ...