Questions tagged [constexpr-function]

42 questions
1
vote
1 answer

c++ how to use constexpr value with operator []

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. But I found a seriously…
blackshadow
  • 77
  • 1
  • 4
0
votes
0 answers

how to make a field of a constexpr object constexpr too?

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.get() always returning 1. class cube { private: …
0
votes
1 answer

Initialize a pointer with constinit

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; // pointer I want to initialize int…
dVNE
  • 161
  • 9
0
votes
1 answer

How to check if instances of a class with a constexpr constructor get instantiated at compile time?

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 struct MyDouble{ double myVal; constexpr MyDouble(double v):…
navid
  • 27
  • 5
0
votes
1 answer

confusion about constexpr function body

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 thread storage duration. All I understood about a…
mada
  • 1,646
  • 1
  • 15
0
votes
1 answer

literal type in constexpr expression and template parameter

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 { public: constexpr Point(double xVal =…
0
votes
1 answer

What is the advantage of constexpr virtual functions in C++20?

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 functions need to be resolved during run-time.…
0
votes
1 answer

static compilation of an instance from json data in rust

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" : "a" } // project code: use…
Avba
  • 14,822
  • 20
  • 92
  • 192
0
votes
1 answer

What is preventing compile time evaluation of this constexpr function?

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 this PortSegment object is created to represent PA2,…
Tagli
  • 2,412
  • 2
  • 11
  • 14
0
votes
2 answers

How can I initialise a constexpr array with values using std::generate

For example, if I wanted a constexpr std::array 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, something like: constexpr std::array a {…
nectarine
  • 65
  • 4
0
votes
1 answer

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 literal, so I cannot simply use a NTTP as a way to…
1 2
3