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
0
votes
0 answers

Confusing behavior with std::is_constructible and if constexpr

Probably easiest to explain if I show an example: #include #include class Unconstructible { public: Unconstructible() = delete; }; int main() { if constexpr (std::is_constructible_v) { …
Nathan Pierson
  • 5,461
  • 1
  • 12
  • 30
0
votes
1 answer

How to match all string types with std::is_same_v

I'm trying to use templates to generify a method. However I'm struggling to match string types like string char * literals. See the following code example: template void match_data_type(ValueType value) { if constexpr…
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
0
votes
0 answers

Best way to fake static_assert inside if constexpr?

As you may know static_assert does not care about if it is inside "active" branch of if constexpr(it always "runs"). I have a hack workaround that uses invalid array dimensions to trigger the error, but it is ugly. Are there better solutions in…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
0
votes
1 answer

Why is unreachable code in constexpr if evaluated?

I had expected the compiler to allow invalid or error statements in unreachable code involving constexpr if: #include #include struct ret_t; struct only_declared_t; auto test = [](auto a) { if…
Markus W.
  • 307
  • 3
  • 10
0
votes
1 answer

c++17 language extension and std:c++17 flag

I'm confused by different errors I have in Visual Studio 2017 (version 15.9.11): 'if constexpr' is a C++17 language extension and language feature 'structured bindings' requires compiler flag '/std:c++17' I know that adding /std:c++17 flag will…
0
votes
1 answer

How can I prevent implicit conversions in std::is_constructible

Let's say, I have a few different classes: class constructible_from_float { public: constructible_from_float(float); }; class constructible_from_double { public: constructible_from_double(double); }; class constructible_from_long_double…
0
votes
2 answers

3 different / same ways of doing N-factorial compile time in C++

I am trying to play with template metaprogramming, constexpr and if constexpr and have come up with 3 different ways of doing a N-recursive / N-factorial operation. All three examples are some I've found here on SO or by searching on the net - and…
nelion
  • 1,712
  • 4
  • 17
  • 37
0
votes
0 answers

Visual-C++ compilation failure if using a class template utilizing if constexpr statements within another class template

I wrote a matrix class template which, amongst other things, includes arithmetic operators which are specialized for certain class template parameters utilizing if constexpr statements. The use of this matrix class within another template class may…
0
votes
2 answers

Extend object lifetime/scope from a `if constexpr` branch

Say we have the following code struct MyClass { MyClass() = delete; // or MyClass() { } MyClass(int) { } void func() { } }; int main() { if constexpr (std::is_default_constructible_v) { MyClass myObj; } else { …
Phil-ZXX
  • 2,359
  • 2
  • 28
  • 40
0
votes
3 answers

constexpr condition not constant?

I wrote the following C++17 code: constexpr bool gDebug = true; template constexpr const T& Select(const bool pCondition, const T& a, const T& b) { if constexpr (pCondition) { return a; } else { return b; …
Silicomancer
  • 8,604
  • 10
  • 63
  • 130
0
votes
2 answers

Why recursive constexpr template value does not compile?

I am defining a way to know the position of the type in a type list, using recursive templates in C++17. I tried two ways : one using constexpr value and one using constexpr function. The second, using if statement, compiles, while the first, using…
Julien Vernay
  • 295
  • 3
  • 13
0
votes
1 answer

MSVS2017 error "expression did not evaluate to a constant" when compile constexpr with lambda

I am using MSVS c++17 and the code below can`t be compiled: #include #include using namespace std; template constexpr auto makeIndices(const Pred &pred) { if…
AeroSun
  • 2,401
  • 2
  • 23
  • 46
0
votes
2 answers

Issue of `if constexpr` with non-placement new

I'm trying to write a utility that invokes either new T{...} or new T(...) based on whether T is an aggregate type. What I have reached so far is as follows. Note that I'm using a macro instead of a function template because of this issue. #define…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
0
votes
2 answers

if constexpr() gives an error in C++17

I have read about constexpr in C++17 using this reference link. Then, I made C++ program to test constexpr : #include int i = 10; int func() { if constexpr (i == 0) return 0; else if (i > 0) return i; else …
msc
  • 33,420
  • 29
  • 119
  • 214
0
votes
1 answer

stop visual studio 17 compile when i hit a bad constexpr if branch

Is there any way to force the compiler to fail if a constexpr if branch is not supposed to be hit? this code below explains it all better than I could: template class arithmetic_type { if constexpr(number_base == 0 ||…
1 2 3
10
11