Questions tagged [if-constexpr]

C++17 feature where the condition of the if statement gets evaluated at compile time. The result will influence which branch will be used. The other branch only needs a correct syntax.

151 questions
3
votes
0 answers

Error case in a series of "if constexpr" statements

I am refactoring a code that previously used SFINAE in C++14 to handle different categories of types (float, ints, strings, etc.). Something like the following: template struct S; template struct…
sunmat
  • 6,976
  • 3
  • 28
  • 44
3
votes
2 answers

Best way to trigger a compile-time error if no if-constexpr's succeed?

I have a long series of if constexpr statements and would like to trigger a compile-time error if none of them succeed. Specifically, I have an abstract syntax tree whose result I would like to convert to a specific set of types that I might need. …
Charles Ofria
  • 1,936
  • 12
  • 24
3
votes
2 answers

getting type index from a type list in C++

When I try and get the index of a type within a list of types using below, the code compiles and returns the correct value when the else clause is used. However when I skip the else clause and place the return getIndex(x + 1); just after…
user3882729
  • 1,339
  • 8
  • 11
3
votes
0 answers

if constexpr gcc bug

I noticed my code base not longer compiling with gcc. I was able to reduce the problem to the following struct bar { int foo(){return 0;} }; int foobar() { if constexpr(true) { return 0; } else { return [](){ …
3
votes
3 answers

How to make the compiler ignore this if-constexpr that evaluates to false?

I am writing a macro that when used to wrap a function call like so: macro(function()), returns a std::optional, where T is the return value of the function. So far it works, but I run into problems when trying to get it to return a…
3
votes
1 answer

Does "if constexpr(something false)" ALWAYS omit template instantiation

Will this template function f() be always NOT instantiated? if constexpr(something false){ //some template function OR function in template class f(); } Below is my test (coliru MCVE). I created fun() that will instantiate E
3
votes
2 answers

if constexpr vs if with constant

As shown in this question: link, if both of if branches are valid, there's no difference between: const int foo = 5; if (foo == 5) { ... } else { ... } and const int foo = 5; if constexpr (foo == 5) { ... } else { ... } in terms…
dabljues
  • 1,663
  • 3
  • 14
  • 30
3
votes
4 answers

Ternary operator and if constexpr

I have situations where sometimes based on some bool I want to call 2 constexpr functions that return different types and assign it to auto constant. Unfortunately ternary operator needs types to be "similar". I have workaround in the code below…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
3
votes
1 answer

From LANGUAGE DESIGN level, why doesn't "if constexpr" decay to "trival if" when condition cannot be deduced at compile-time

As we know, when constexpr function's return value cannot be known at compile-time, it will be delayed to be computed at run-time(IOW, decay to non-constexpr function). This allows us to adhere constexpr to a function freely and need not worry about…
Chen Li
  • 4,824
  • 3
  • 28
  • 55
3
votes
1 answer

When does a constexpr static member stop being a constexpr?

I have this snippet. #include #include struct JustStr { JustStr(const std::string& x) : val(x) {} static constexpr bool pred = false; std::string val; }; template class C { private: T x; public: …
fakedrake
  • 6,528
  • 8
  • 41
  • 64
3
votes
2 answers

Using constexpr-if in a generic lambda to determine type of a parameter

I have the following problem: I have a class hierarchy with a base class and two sub-classes. I have implemented a resolve_type function that accepts an instance of the base class and a generic lambda (or similar). The function resolves its type and…
maxj
  • 45
  • 4
3
votes
1 answer

Using constexpr to validate literal parameters in constructor

I started experimenting with constexpr. What I'm trying to achieve is to validate literal numeric values provided as ctor parameters. I started with the following, throwing if constructing MyStruct with a value <= 4. constexpr int validate(int v) { …
Stefano Azzalini
  • 1,027
  • 12
  • 21
2
votes
1 answer

Function that can receive any of T, T&, and T&& as input, AND also recognize its type?

I want to create a function that correctly recognizes the type of its parameter. template void test(T&& t){ if constexpr(std::is_same_v){ std::cout<< "= int"<
cppBeginner
  • 1,114
  • 9
  • 27
2
votes
1 answer

Configure CMake to use newest available C++ standard

I'm building a C++ library with CMake. Is it possible to set the C++ standard to the newest released standard supported by the user's compiler? I checked the docs for the variable CXX_STANDARD, but it only shows how to use one specific…
2
votes
2 answers

Why is always_false_v required in this situation?

I'm using std::variant to specify the types of properties that an entity in my project may have, and stumbled upon this code from cppreference: std::visit([](auto&& arg) { using T = std::decay_t; if…
CakePlusPlus
  • 943
  • 7
  • 15