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
4
votes
2 answers
Evaluating an 'if' clause at compile time
Consider the following code snippet:
#include
#include
void g(unsigned) {
// ...
}
template
void f(UIntT n)
{
if constexpr (std::numeric_limits::max() > std::numeric_limits::max())
…

plexando
- 1,151
- 6
- 22
4
votes
2 answers
Conditionally creating members inside a class
Is it possible to do something like:
template
class Object
{
public:
if constexpr ((majorVer == 1) && (minorVer > 10))
bool newField;
else
int newInt
};
or
template

Arun
- 3,138
- 4
- 30
- 41
4
votes
1 answer
In what sense is std::disjunction short-circuiting at compile_time
From the description on cppreference.com, I was under the impression that std::disjunction is designed to give me short-circuiting at compile-time, so that I can use it like this:
#include
#include
template

x432ph
- 400
- 1
- 10
4
votes
2 answers
`if constexpr` vs `if` in light of compiler optimization and code performance
Consider a function template func that is very performance critical. It can be instantiated with T=Type1 or some other type. Part of the function logic depends on T it is instantiated with.
One can either explicitly use a if constexpr (Code B) or…

Anton Menshov
- 2,266
- 14
- 34
- 55
4
votes
2 answers
if vs if constexpr inside constexpr function
Recently I modify some if constexpr into if in my constexpr functions and found they still work fine and can be evaluated when compile time. Here is a minimum case:
template
constexpr bool is_negative()
{
if constexpr (N >= 0) return…

Chen Li
- 4,824
- 3
- 28
- 55
4
votes
0 answers
Why does "if constexpr" not behave as expected in such a case?
#include
#include
void f()
{}
#define M1(...) \
if constexpr (std::is_void_v)\
{ std::cout << "is_void" << std::endl; }\
else\
{ std::cout << "is_not_void" << std::endl; }
#define M2(...) \
if…

xmllmx
- 39,765
- 26
- 162
- 323
4
votes
2 answers
if constexpr and C4702 (and C4100, and C4715)
Is there a way to fix the following problem:
This code produces a C4702 warning 'unreachable code' (on VC++ 15.8 with /std:c++17)
template
inline bool MatchMonostate( VariantType& variant )
{
SUPPRESS_C4100(…

old123987
- 121
- 6
4
votes
2 answers
Alternatives to many nested std::conditional_t?
I find many nested std::conditional_t hard to read so I choose a different pattern(of calling decltype on function with auto return type):
template
auto find_int_type(){
static_assert(sizeof(int)==4);
…

NoSenseEtAl
- 28,205
- 28
- 128
- 277
4
votes
2 answers
disable branch with "if constexpr" and SFINAE
I want to enable/disable branches at compile time depending of whether a function can be called with certain arguments.
What has to go in the if constexpr condition?
I can get the result type via std::result_of(decltype(add)(A, B)), but how can I…

jfrohnhofen
- 167
- 9
4
votes
1 answer
not-constexpr variable in if constexpr – clang vs. GCC
struct A{
constexpr operator bool()const{ return true; }
};
int main(){
auto f = [](auto v){ if constexpr(v){} };
A a;
f(a);
}
clang 6 accepts the Code, GCC 8 rejects it with:
$ g++ -std=c++17 main.cpp
main.cpp: In lambda…

Benjamin Buch
- 4,752
- 7
- 28
- 51
4
votes
1 answer
Constexpr if with non-template types
#include
int foo(int x) {
if constexpr (std::is_same_v) {
x = std::string();
}
}
int main(void)
{ return 0; }
This code doesn't compile on either GCC 7 nor Clang 5:
error: cannot convert…

Maël Nison
- 7,055
- 7
- 46
- 77
3
votes
2 answers
`if constexpr` in fold expression
I am trying to make a fold expression in a function which populates the outgoing parameters of a function with some values that come in from a string vector. My fold expression is like:
((if constexpr (std::is_integral_v)
{
args =…

Ferenc Deak
- 34,348
- 17
- 99
- 167
3
votes
1 answer
if constexpr block not compiling
I have a class that can inherit from an empty struct or from a struct with some members, depending on a bool. Using that same bool, I am adding an if constexpr block to access the members of the base class, but I am getting a compiler error
struct…

jjcasmar
- 1,387
- 1
- 18
- 30
3
votes
2 answers
#if Vs if constexpr
Which one is more appropriate for compile-time configurations (such as debug/release), preprocessor directives, or if constexpr?
#define DBG
#if DBG
// some code
#endif
// --------------------------------- or
inline constexpr bool DEBUG { true…

digito_evo
- 3,216
- 2
- 14
- 42
3
votes
1 answer
Is there a constexpr which lets me determine if there is an output operator (<<) for a particular type?
In order to prevent the compiler to apply e.g. a std::vector to a statement like std::cout << u, I would like to do something like this:
if constexpr (std::has_output_operator) {
std::cout << u;
}
Is there some way to achieve this?
EDIT…

user1479670
- 1,145
- 3
- 10
- 22