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
2
votes
1 answer
c++ convert fmt::format_stringto std::string_view
I'm currently struggling to convert fmt::format_stringto a std::string_view.
The idea: I would like to create a fucntion with can be called from an ISR and task context. However in an ISR no dynamic memory allocation is allowed. That's why…

JHeni
- 455
- 3
- 12
2
votes
0 answers
Given `false_v = false`, `static_assert(false_v)` compiles inside `if constexpr (false)`. Is this legal?
Consider this code that static asserts false inside an if constexpr (false) statement.
#include
template
struct False {
static constexpr bool value = false;
};
template
struct Foo {
void foo() {
…

jcarpenter2
- 5,312
- 4
- 22
- 49
2
votes
1 answer
Convert static_assert in if constexpr for type checking to C++14
I am working to downgrade a C++ 17 project to C++ 14. I found the following code:
#include
#include
#include
template
void get_assertion_message()
{
static_assert(flag, "unknown…

kiner_shah
- 3,939
- 7
- 23
- 37
2
votes
2 answers
Convert if constexpr based C++17 templatized code to C++14
I am working on downgrading a project written in C++ 17 to C++ 14. While downgrading, I came across a piece of code involving if constexpr and I wish to convert it to C++ 14 (From what I know, if constexpr is a C++ 17 feature).
Boost's is_detected…

kiner_shah
- 3,939
- 7
- 23
- 37
2
votes
1 answer
Is it an ODR violation to have an inline templated function have different behavior due to if constexpr?
It's possible to detect if a type is complete https://devblogs.microsoft.com/oldnewthing/20190710-00/?p=102678
I have reason to want to provide a different (inlinable) implementation if a type is complete.
Is it an ORD violation for a templated…

Ben
- 9,184
- 1
- 43
- 56
2
votes
1 answer
Inline function with one of two parameters as constexpr
Assume I have a function with two parameters where first parameter is dynamic but second parameter is always constant known at compile time:
uint8_t convert_bcd(uint8_t num, uint8_t mask) {
uint8_t result = mask & 0x0F & num;
if constexpr…

Sergey Kostrukov
- 1,143
- 15
- 24
2
votes
1 answer
typeid vs std::is_same vs if constexpr
I'm confused by these three things. Here is a simple example:
template
void func(T t) {
if (typeid(T) == typeid(int)) {
std::cout << "f - int" << std::endl;
} else {
std::cout << "f - other" << std::endl;
…

Yves
- 11,597
- 17
- 83
- 180
2
votes
2 answers
Initialization of static bool with lambda and constexpr-if gives adress error
I was trying to implement a certain type trait, but faced this following error.
Here is a simple program that shows the behavior:
#include
template
struct my_floating_point // just to make a case
{
// static constexpr…

Bbllaaddee
- 145
- 1
- 9
2
votes
1 answer
How to check in compile time that the function has default parameter value?
All try to do is to make this piece of code
int main() {
if constexpr( ??? ) {
std::cout << "Yes\n";
std::cout << f() << '\n';
std::cout << f(42) << '\n';
}
else {
std::cout << "No\n";
}
…

Yuki Endo
- 192
- 1
- 7
2
votes
4 answers
How to cause static error in constexpr if-else chain?
In the following C++20 function template:
template
void f() {
if constexpr (i == 1)
g();
else if constexpr (i == 2)
h();
else
??? // <--error
}
Is there something we can write in ??? such that a call of…

Andrew Tomazos
- 66,139
- 40
- 186
- 319
2
votes
2 answers
Constexpr if testing one template parameter
If I have a class that has two template parameters, is there any way to branch an if constexpr on just one of those parameters? In the following example I can test if both parameters match, but I would like a single way of matching any version of…

Iain Brown
- 1,079
- 3
- 11
- 23
2
votes
1 answer
if constexpr equivalent for switch
Since C++17, a template function can return one type or another in function of an expression evaluated at compile-time:
template
constexpr auto f()
{
if constexpr (N == 1)
return 1.0; // return an double
else if constexpr…

mfnx
- 2,894
- 1
- 12
- 28
2
votes
1 answer
How can I merge this three c++ template function into one by universal reference or std::forward?
I've got these codes blow
template
struct CustomF1 final
{
void operator()(T *p) const
{
if (p)
Fn(p);
}
};
template
struct CustomF2 final
{
void operator()(T p) const
{
…

user392412
- 743
- 12
- 18
2
votes
2 answers
Wrong understanding of 'if constexpr'
I have following code
static constexpr bool condition = true;
int square(int num) {
if constexpr (condition) {
return num * num;
} else {
x
return num;
}
}
int main() {
return square(3);
}
compiled…

ge45mue
- 677
- 8
- 23
2
votes
1 answer
why this constexpr if doesn't compile
I want to simplify a code by using a constexpr function instead of multi constexpr if branches.
This is the code with the old code commented
the old code compiles with msvc (vs 2017 with c++17) and clang (android ndk r20), but it fails to compile…

dev65
- 1,440
- 10
- 25