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
7
votes
1 answer

"if constexpr" interaction with "try in constexpr function" warning

I claim that this program ought to be well-formed: it declares a constexpr member function of S. However, both GCC and Clang reject this program. template struct S { constexpr int foo() { if constexpr (std::is_same_v
Quuxplusone
  • 23,928
  • 8
  • 94
  • 159
6
votes
1 answer

Why does the false branch of "if constexpr" get compiled?

Why is this code giving error while compiling? My knowledge (and also this) of "if constexpr" says the else block shouldn't get compiled. if constexpr (true) { int a = 10; } else { int b = 10 } The error is: error: expected ‘,’ or ‘;’…
6
votes
2 answers

How to decide constexpr to return a reference or not

If you have a function that if constexpr () decides to do one thing or the other, how to return an lvalue in one case and an rvalue in the other case? The following example does not compile in the first usage line, because the return type auto is no…
Aart Stuurman
  • 3,188
  • 4
  • 26
  • 44
6
votes
2 answers

if constexpr instead of tag dispatch

I want to use if constexpr instead of tag dispatching, but I am not sure how to use it. Example code below. template struct MyTag { static const int Supported = 0; }; template<> struct MyTag { static const int…
0xBADF00
  • 1,028
  • 12
  • 26
6
votes
2 answers

Possible template & constexpr–if incompatibility

I wanted to calculate e value at compile–time (don't worry, not a homework), yet something went wrong. template, size_t factorial = 1, size_t count = 1> constexpr double e_impl() { if…
Kamil Koczurek
  • 696
  • 5
  • 15
6
votes
1 answer

if constexpr(condition) as compile-time conditional

I want to use a constexpr bool (useF in the example below) to enable a feature in the following code. Here, calling A::f(). Additionally, I want to be the alias-template (a) to be void in the case I switch off the feature. I tried to use a constexpr…
wimalopaan
  • 4,838
  • 1
  • 21
  • 39
5
votes
3 answers

Why does min still complain inside constexpr?

I have the following code snippet: #include #include #include #include using T = double; int main() { f(); } void f() { T x = 2; if constexpr(std::is_integral_v) { std::cout…
user122049
  • 397
  • 1
  • 11
5
votes
2 answers

Does C++ 17 have static reflection?

Combining if constexpr with some entities, in C++17, I'm able to inspect types at compile time. Can these techniques be considered static reflection? Or is it just type inspection? Example: if constexpr (std::is_same_v)…
João Paulo
  • 6,300
  • 4
  • 51
  • 80
5
votes
3 answers

Stop compilation if `if constexpr` does not match

I have a template function that checks the type of a template argument with an if constexpr such as template bool something(T arg) { if constexpr (std::is_integral_v) { return true; } else { // What can I write here so…
Marten
  • 1,336
  • 10
  • 16
5
votes
1 answer

Constexpr operator new

Is it possible to overload the operator new to be constexpr function? Something like: constexpr void * operator new( std::size_t count ); The reason why would be to execute constexpr function within the overloaded operator body where count argument…
Martin Kopecký
  • 912
  • 5
  • 16
5
votes
1 answer

if constexpr inside lambda, differing compiler behavior

Consider the following code. If my understanding of if constexpr is correct, the else branch should not be compiled so the z() should not be considered an error. #include struct Z{}; template void f(T z) { auto lam =…
jcai
  • 3,448
  • 3
  • 21
  • 36
4
votes
2 answers

strange behaviour of operator && in constexpr context

The following code tries to make compile-time decisions based on the last argument passed in a parameter pack. It contains a comparison if the number of parameter-pack arguments is > 0 and then tries to get the last element of it. However, the…
glades
  • 3,778
  • 1
  • 12
  • 34
4
votes
0 answers

Function template specialization vs if-constexpr

Is there a conceptual (or other) difference between specializing function template and using if constexpr? E.g.: template void fun(T t) { // Generic code. } template<> void fun(int t) { // int code. } Vs. template void…
user2052436
  • 4,321
  • 1
  • 25
  • 46
4
votes
3 answers

How to compare string_view using if-constexpt in a constexpr context

Is it possible to compare std::string_view using "if constexpr" in a constexpr context? And why is_hello_2 and is_hello_4 fail to compile showing error: "‘s’ is not a constant expression" static constexpr bool is_hello_1(auto s) { return s ==…
Equod
  • 556
  • 3
  • 13
4
votes
1 answer

Problem with requires clause inside if constexpr

While trying to implement if constexpr with requires clause based on if constexpr and requires-expression for ad-hoc concepts checking facing the following problem: template concept TuplePair = requires(P p) { requires…
Amir Kirsh
  • 12,564
  • 41
  • 74
1 2
3
10 11