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

if constexpr Seems to Only Work if Both Cases are Valid

Given the following code: template constexpr remove_reference_t has_x() {return true;} template constexpr bool has_x(U...) {return false;} class A { public: int x; }; int main() { …
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
2
votes
1 answer

Why if constexpr fails to bypass constexpr evaluation?

I'm building a static loop for type dispatching using macros. Here is what I achieved so far. #define LOOP(n, f) \ static_assert(n <= 8 && "static loop size should <= 8"); \ do { …
Amos
  • 3,238
  • 4
  • 19
  • 41
2
votes
2 answers

Instantiating template function in a false if constexpr gives error

Consider the following program: #include template constexpr int constexprValue(Params_t...) { return 5; } int main() { const bool flag = true; if constexpr(flag) { constexpr int value =…
Warp
  • 363
  • 2
  • 9
2
votes
2 answers

"Expected a statement" in constexpr if else expression

I have a function test, which prints out the underlying type of an enum parameter: enum class TestEnum : uint32_t { }; template void test(TEnum v) { // Line 12 if constexpr…
Silverlan
  • 2,783
  • 3
  • 31
  • 66
2
votes
1 answer

LLVM coverage confused by if-constexpr

I have encountered a weird problem with LLVM coverage when using constant expressions in an if-statement: template int foo(const T &val) { int idx = 0; if constexpr(std::is_trivially_copyable::value && sizeof(T) <=…
Resurrection
  • 3,916
  • 2
  • 34
  • 56
2
votes
1 answer

C++ 17: How to call a different constructor using if constexpr?

Say I have a class that takes a boolean in its constructor and depends on the value of the boolean, if calls different functions. class MyClass { MyClass(bool is_second) { common_code(); if (!is_second) …
motam79
  • 3,542
  • 5
  • 34
  • 60
2
votes
0 answers

Possible VC++17 compiler bug with if constexpr?

While playing around with some of the new C++17 features in Visual C++ - I came across an issue which to me seems like a bug. The code seems to generate as expected when compiling under other compilers using compiler explorer. The error that…
Floris
  • 55
  • 2
1
vote
3 answers

constexpr switch statements in C++

Do we have a switch constexpr support in C++? For context, we all know that we have if constexpr for compile-time if statements. Those if-statements will have very low overhead as compiler will evaluate them at compile time. We (or in particular I)…
mgNobody
  • 738
  • 7
  • 23
1
vote
0 answers

compiling/non-compiling discarded if constexpr sub-statements in templates

I was reading cppreference page for If statements, as well as C++17 standard (draft) and I also found out a (non exhaustive) pre-existing question on stackoverflow. What I understand is that, within a template, a discarded sub-statement of an if…
1
vote
2 answers

constexpr base case not considered during compilation

I was trying out the constexpr functions and stumbled upon the below example of implementing Fibonacci numbers There is no logical difference between fibon2 and fibon1 but I still get the compilation error of exceeding template initializations for…
user3703826
  • 87
  • 1
  • 6
1
vote
1 answer

Is it possible to use 'if constexpr' to determine if a template function could be instantiated with a particular type parameter?

Suppose there exists the following code: class Foo { public: void foo() const { std::cout << "foo" << std::endl; } }; class Bar { public: void bar() const { std::cout << "bar" << std::endl; } }; template void DoFoo(const T&…
Tyler McHenry
  • 74,820
  • 18
  • 121
  • 166
1
vote
0 answers

`if constexpr` with `template` arguments vs. with `constexpr` expressions

Consider the following definition: template struct Foo; template<> struct Foo<1> { void fn1(); }; template<> struct Foo<2> { void fn2(); }; Foo<1> has a member function fn1 and similarly Foo<2> has fn2. We can use these functions, selecting…
geometrian
  • 14,775
  • 10
  • 56
  • 132
1
vote
2 answers

How can I static assert to disallow "mixed endianness" in a non-templated member function

I am using 2 x std::uint64_t and 1 x std::uint32_t in a high performance implementation of of operator<=> in a struct conataining a std::array. I am trying to make it cross compiler and architecture compatible. As part of that I am…
Oliver Schönrock
  • 1,038
  • 6
  • 11
1
vote
1 answer

SFINAE User-Defined-Conversion Operator

I'm trying to do a templated user-defined-conversion that uses design by introspection. In C++20 I can do the following: template operator T() const { if constexpr( requires { PFMeta::from_cursor(*this); } ) …
perryperry
  • 33
  • 10
1
vote
0 answers

Ternary operator works with constexpr expressions but `if constexpr` does

I am playing around in C++17 with clang version 13.0.0 to test whether a given constexpr value is NaN with wrappers but I keep getting errors with if constexpr but the compiler passes using ternary operator template constexpr bool…
Kish
  • 293
  • 1
  • 6