Skip to main content
Filter by
Sorted by
Tagged with
1 vote
1 answer
205 views

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-...
Oersted's user avatar
  • 3,834
6 votes
4 answers
283 views

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) == ...
abcdefg's user avatar
  • 4,637
2 votes
1 answer
243 views

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 ...
user31597808's user avatar
0 votes
1 answer
128 views

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, ...
sh1's user avatar
  • 5,010
4 votes
1 answer
205 views

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 ...
Oersted's user avatar
  • 3,834
3 votes
0 answers
62 views

Here is the test code: #include <type_traits> enum class ShaderType:unsigned{ Vertex = 0, Fragment = 1, }; template<ShaderType type> struct ShaderTypeHolder{ static constexpr ...
Shenghua Lin's user avatar
2 votes
2 answers
113 views

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, ...
AR-47's user avatar
  • 23
0 votes
1 answer
299 views

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 ...
Bahram Pouryousefi's user avatar
1 vote
3 answers
294 views

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 ...
PaperBirdMaster's user avatar
2 votes
1 answer
294 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 or to-be C++26, as far as supported by either G++ oder Clang?...
Torsten Knodt's user avatar
0 votes
1 answer
88 views

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. ...
vohir's user avatar
  • 23
1 vote
2 answers
217 views

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. ...
NormakLOE32's user avatar
9 votes
2 answers
1k views

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 ...
Oersted's user avatar
  • 3,834
0 votes
1 answer
142 views

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. ...
ntos's user avatar
  • 379
0 votes
1 answer
172 views

[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 ...
John Kalane's user avatar
  • 1,195
0 votes
1 answer
104 views

Edit at 10/10/2023 06:30 UTC: Thanks to valuable comments from @DrewDormann, @Solomon Slow, @Pepijn Kramer, and @user17732522, I've come to realize that I posed a question without thorough ...
yqZhang4480's user avatar
3 votes
1 answer
161 views

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 ...
Waker's user avatar
  • 245
3 votes
1 answer
763 views

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 ...
Oersted's user avatar
  • 3,834
2 votes
1 answer
147 views

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 ...
Satyam Sharma's user avatar
4 votes
0 answers
470 views

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 ...
user51462's user avatar
  • 2,092
3 votes
1 answer
291 views

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 ...
jsfpdn's user avatar
  • 83
0 votes
0 answers
81 views

cube is a class that from what i know can be both constexpr and not and for some reason c.get() is not constexpr because the second cout prints 5 which mean it changed the value of c to 5 instead of c....
shar yashuv Giat's user avatar
10 votes
1 answer
2k views

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 ...
Brotcrunsher's user avatar
  • 2,304
2 votes
3 answers
273 views

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: #...
jabluko's user avatar
  • 33
1 vote
2 answers
106 views

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 ...
AK87's user avatar
  • 623
2 votes
2 answers
160 views

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 ...
Andi Abrudan's user avatar
2 votes
2 answers
143 views

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 ...
lomo's user avatar
  • 23
4 votes
1 answer
335 views

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(...
Finn Eggers's user avatar
1 vote
0 answers
46 views

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 ...
std_unordered_map's user avatar
5 votes
1 answer
924 views

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 ...
Carlo Wood's user avatar
  • 7,155
6 votes
3 answers
408 views

Is there a way to write a constexpr function that returns how deep a std::vector is nested? Example: get_vector_nested_layer_count<std::vector<std::vector<int>>>() // 2 ...
palapapa's user avatar
  • 1,051
2 votes
0 answers
706 views

I'm trying to use the resources of a temporary class object as a template parameter. But apparently this doesn't work: godbolt #include <iostream> constexpr size_t size(const char* s) { int ...
glades's user avatar
  • 5,374
2 votes
1 answer
167 views

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 ...
bun9's user avatar
  • 201
0 votes
1 answer
259 views

I was wondering whether I could initialize a pointer with constinit in C++20, and I didn't find any adequate answer on the internet. I have a simple code like this: struct a { const char *s; // ...
dVNE's user avatar
  • 161
0 votes
1 answer
162 views

How can I check that instances of MyDouble will be created at compile time? What will happen if I instantiate MyDouble with a non-constant expression? #include <iostream> struct MyDouble{ ...
navid's user avatar
  • 27
0 votes
1 answer
475 views

cppreference said the following about the body of a constexpr function: the function body must not contain: a definition of a variable of non-literal type a definition of a variable of static or ...
mada's user avatar
  • 2,033
1 vote
1 answer
135 views

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 ...
Chris G.'s user avatar
  • 846
9 votes
1 answer
1k views

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) { ...
dtldarek's user avatar
  • 989
0 votes
1 answer
209 views

Why do I can use non constexpr literal types in constexpr functions(such as reflection) and it can be returned as constexpr, but I can't use such types in template non-type parameters? class Point { ...
Vanconts's user avatar
3 votes
2 answers
1k views

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). #...
MiCo's user avatar
  • 401
5 votes
1 answer
892 views

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 . ...
digito_evo's user avatar
  • 3,735
5 votes
1 answer
1k views

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 ...
absuu's user avatar
  • 386
2 votes
1 answer
1k views

I can easily say that by declaring a function as constexpr, we evaluate it during the compile-time and this saves time during run-time as the result was already produced. On the other hand, virtual ...
Caglayan Dokme's user avatar
4 votes
3 answers
1k views

I have code something like this: template<typename ... Args> constexpr size_t get_init_size(Args ... args) { return sizeof...(Args); } template<typename ... Args> constexpr auto ...
emik_g's user avatar
  • 109
1 vote
1 answer
2k views

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 ...
Radu C's user avatar
  • 359
1 vote
1 answer
371 views

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:...
DiKetarogg's user avatar
1 vote
1 answer
1k views

I want to create a static instance at compile time (not runtime) from JSON data loaded from a file (similar to constexpr): Example: // a file in the project /path/to/project/data.json { "field1&...
Avba's user avatar
  • 15.4k
4 votes
3 answers
598 views

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 ...
Victor's user avatar
  • 470
0 votes
1 answer
289 views

I'm working on a class for representing a set of hardware pins of a microcontroller (STM32). The selected pins may be discontinuous on the port, but they are assumed to be ordered. For example, if ...
Tagli's user avatar
  • 2,662
1 vote
1 answer
132 views

The origin problem is I want to use const char* or char [] in template non-type arguments. Of course it is not supported now. So I want to write some code to convert char[] to std::integer_sequence. ...
 blackshadow's user avatar