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

if constexpr in a recursive generic lambda: different compiler behavior

The following code compiles successfully with g++ 7.3.0 and fails to compile with clang++ 6.0.0 (compilation flags are -std=c++17 -Wall -Wextra -Werror -pedantic-errors): auto foo = [](auto, auto... tail) { if constexpr (sizeof...(tail) > 0) …
Constructor
  • 7,273
  • 2
  • 24
  • 66
15
votes
2 answers

Structured bindings for your own type that isn’t a struct or a tuple(via public member function)

I am going through the Herb Sutter's A journey: Toward more powerful and simpler C++ programming Structure Binding section In order to understand the concept .Best is to write a program I tried but getting some error Just want to try how to use…
Hariom Singh
  • 3,512
  • 6
  • 28
  • 52
15
votes
2 answers

Can `if constexpr` be used to declare variables with different types and init-expr

For example: void foo() { if constexpr (...) int x = 5; else double x = 10.0; bar(x); // calls different overloads of bar with different values } It's common case in D lang, but I didn't found info about C++17. Of…
aaalex88
  • 619
  • 4
  • 13
13
votes
2 answers

Why does if constexpr require an else to work?

I am trying to use if constexpr in the following way: template