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
77
votes
1 answer
Do I need to put constexpr after else-if?
Inspired by this answer, I tried to copy and paste (and add testing in main()) this code:
template
std::tuple foo(T a) {
if constexpr (std::is_same_v)
return {a, 0.0};
else if (std::is_same_v

Fureeish
- 12,533
- 4
- 32
- 62
69
votes
3 answers
Equivalent ternary operator for constexpr if?
Maybe I missed something, but I can't find any hints: is there a constexpr ternary operator in C++17 equivalent to constexpr-if?
template
class BusAddress {
public:
explicit constexpr BusAddress(Address device) :
…

wimalopaan
- 4,838
- 1
- 21
- 39
42
votes
1 answer
"constexpr if" vs "if" with optimizations - why is "constexpr" needed?
C++1z will introduce "constexpr if" - an if that will have one of branches removed, based on the condition. Seems reasonable and useful.
However, is it not possible to do without constexpr keyword? I think that during compilation, compiler should…

MateuszL
- 2,751
- 25
- 38
39
votes
3 answers
C++ check if statement can be evaluated constexpr
Is there a method to decide whether something can be constexpr evaluated, and use the result as a constexpr boolean? My simplified use case is as follows:
template
class derived
{
template
void do_stuff() { (...)…

Aart Stuurman
- 3,188
- 4
- 26
- 44
39
votes
2 answers
How do I use concepts in if-constexpr?
How does one use concepts in if constexpr?
Given the example below, what would one give to if constexpr to return 1 in case T meets the requirements of integral and else 0?
template
concept integral = std::is_integral_v;
struct…

nnolte
- 1,628
- 11
- 25
38
votes
4 answers
Why doesn't this "undefined extern variable" result in a linker error in C++17?
I have compiled and ran the following program in a C++17 compiler (Coliru). In the program, I declared an extern variable, but did not define it. However, the compiler doesn't give a linker error.
#include
extern int i; // Only…

msc
- 33,420
- 29
- 119
- 214
31
votes
3 answers
Why doesn't an if constexpr make this core constant expression error disappear?
In reference to this question. The core constant expression that is used to initialize the constexpr variable y is ill-formed. So much is a given.
But if I try to turn the if into an if constexpr:
template
void foo() {
constexpr int…

StoryTeller - Unslander Monica
- 165,132
- 21
- 377
- 458
28
votes
1 answer
An 'if constexpr branch' does not get discarded inside lambda that is inside a template function
The following code:
#include
struct X {
static constexpr void x() {}
};
template
constexpr bool makeFalse() { return false; }
template
void foo() {
T tmp;
auto f = [](auto type) {
…

nicolai
- 1,140
- 9
- 17
28
votes
5 answers
"If constexpr" in C++17 does not work in a non-templated function
I tried to play with the C++17 standard. I tried to use one of the features of C++17 if constexpr. And I had a problem... Please take a look at the following code. This compiles without errors. In the following code, I tried to use if constexpr to…

NYM
- 309
- 3
- 5
26
votes
2 answers
std::is_constant_evaluated behavior
GCC9 already implements std::is_constant_evaluated. I played a little bit with it, and I realized it is somewhat tricky. Here’s my test:
constexpr int Fn1()
{
if constexpr (std::is_constant_evaluated())
return 0;
else
return…

metalfox
- 6,301
- 1
- 21
- 43
22
votes
2 answers
Constexpr variable captured inside lambda loses its constexpr-ness
This code compiles fine in g++ (coliru), but not MSVC (godbolt and my VS2017).
#include
#include
template void f(){
constexpr bool b=std::is_same_v; //#1
auto func_x=[&](){
if constexpr(b){…

javaLover
- 6,347
- 2
- 22
- 67
21
votes
3 answers
Something like "if constexpr" but for class definition
if constexpr is a big step for getting rid of preprocessor in C++ programs. However it works only in functions - like in this example:
enum class OS
{
Linux,
MacOs,
MsWindows,
Unknown
};
#if defined(__APPLE__)
constexpr OS os =…

PiotrNycz
- 23,099
- 7
- 66
- 112
19
votes
1 answer
Accessing member type with `if constexpr` inside generic lambda requires both branches to be well-formed - gcc vs clang
Consider two structs with different member type aliases:
struct foo { using x = int; };
struct bar { using y = float; };
Given a T in a template context, I want to get either T::x or T::y depending on what T is:
template
auto s()
{
…

Vittorio Romeo
- 90,666
- 33
- 258
- 416
16
votes
1 answer
if constexpr - why is discarded statement fully checked?
I was messing around with c++20 consteval in GCC 10 and wrote this code
#include
#include
#include
template
consteval std::optional…

Yamahari
- 1,926
- 9
- 25
15
votes
3 answers
If there's an if-constexpr, how come there's no switch-constexpr?
In C++17, if constexpr was introduced; however, there doesn't seem to be a switch constexpr (see here). Why is that? That is, if a compiler supports if constexpr, is it not also trivial for it to support switch constexpr (at worst as an…

einpoklum
- 118,144
- 57
- 340
- 684