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.
Questions tagged [if-constexpr]
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 class Trait,
typename First, typename Second, typename... Rest>
constexpr bool binaryTraitAre_impl()
{
if constexpr (sizeof... (Rest)…

Chen Li
- 4,824
- 3
- 28
- 55
12
votes
1 answer
How to properly use concepts?
Currently, I am learning C++ and decided just start with C++20. However, these codes are driving me crazy, since I don't think the result makes any sense.
The following code will have the sentence Valid array. printed. What I meant above is that…

Hydrogen
- 301
- 1
- 8
11
votes
1 answer
Nested constexpr-if statement in discarded branch is still evaluated?
It appears to me that a constexpr-if statement that is inside the discarded branch of another constexpr-if statement is evaluated in MSVC (version 15.7.3).
Consider the following code:
#include
#include
template
int…

Bernard
- 5,209
- 1
- 34
- 64
11
votes
1 answer
False-branch of if constexpr not discarded in templated lambda
I have a problem with "if constexpr" in a templated lambda. For the sake of argument let's ignore how I got there, but I have a struct foo that is defined in some way to result in something as follows:
template
struct foo {
int…

Hanno Bänsch
- 284
- 3
- 13
11
votes
1 answer
Can constexpr-if-else bodies return different types in constexpr auto function?
I'm trying to write a function that maps an enumeration of values to a set of types based on the runtime value of the enumeration. I realize that you cannot return different types based on the runtime value of an enumeration because the compiler…

Short
- 7,767
- 2
- 25
- 33
10
votes
1 answer
std::is_member_function_pointer does not compile if false
What I am looking for: I have a templated class and want to call a function if the class has the wanted function, something like:
template do_something() {
if constexpr (std::is_member_function_pointer::value) {
…

jagemue
- 363
- 4
- 16
9
votes
1 answer
Errors when using constexpr-if: expected '(' before 'constexpr'
I am trying to use if-constexpr to check something, but I encounter errors like
expected '(' before 'constexpr'
'else' without a previous 'if' "
So far i check there is nothing wrong with my codes
My compiling flag is g++ -std=c++17…

Lim Ta Sheng
- 371
- 3
- 8
9
votes
0 answers
Static_assert inside if constexpr not discarded
The following static_assert should be discarded being in the false branch of if constexpr, yet compilation fails due to assert failure:
#include …

davide
- 2,082
- 3
- 21
- 30
8
votes
1 answer
Is "if constexpr" useful outside of templates?
I'm trying to understand if constexpr fully.
I understand, that if if constexpr(expr) used in a template, and expr is dependent on a template parameter, then during instantiation, only one of the then/else branches will be instantiated, the other…

geza
- 28,403
- 6
- 61
- 135
8
votes
1 answer
MSVC swallows const from fundamental template parameter in variadic template methods using constexpr if
I have a problem that I'm almost certain is a MSVC bug, but maybe I'm missing something.
Here is a simplified version of the actual code:
template
class InnerType {};
template
class OuterType {
public:
void…

Sebastian.M
- 646
- 6
- 18
8
votes
3 answers
constexpr in for-Statement
c++17 provides if constexpr, in which:
the value of condition must be a contextually converted constant expression of type bool. If the value is true, then statement-false is discarded (if present), otherwise, statement-true is discarded
Is there…

Jonathan Mee
- 37,899
- 23
- 129
- 288
7
votes
3 answers
Is this use of static_assert inside if constexpr well-formed?
I read a couple of answers yesterday, on the use of static_assert(false, "Some message") inside the else clause of an if constexpr. I understand that it's considered to be ill-formed, according to the standard (even if some compilers, including…

bjaastad_e
- 691
- 6
- 10