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
1
vote
2 answers

Comparing constexpr function parameter in constexpr-if condition causes error

I'm trying to compare a function parameter inside a constexpr-if statement. Here is a simple example: constexpr bool test_int(const int i) { if constexpr(i == 5) { return true; } else { return false; } } However, when I compile this with GCC 7…
GoogieK
  • 371
  • 2
  • 14
0
votes
3 answers

How can a constructor signal to caller, whether an exception it is throwing is fatal?

I'm dealing with an application, that needs to read in 10+ CSV-files (of different kind) as input. The data is read into a container -- std::map or vector. Previously each kind had its own parsing function, and I'm working on unifying that into a…
Mikhail T.
  • 3,043
  • 3
  • 29
  • 46
0
votes
1 answer

Print TCHAR string to std::ostream when TCHAR can map to either char or wchar_t

I'm reusing the same exact same source file (for command-line parsing), in different executables. I don't use a library, and instead build that same source directly into each exe. Recently I added an option to list all shared-libraries used by the…
ddevienne
  • 1,802
  • 2
  • 17
  • 28
0
votes
1 answer

constexpr guard clause does not compile

I wanted to add a constexpr guard clause in my code in order to avoid unnecessary indentation, but ran into this issue. This compiles: #include #include template void inc(T& t) { if constexpr…
Fredrik Enetorp
  • 414
  • 4
  • 15
0
votes
0 answers

populate in-class c-style array using constexpr at compile time

I have a class to calculate CRC using table maethod that looks like this (it is incomplete): template requires…
lazba
  • 129
  • 9
0
votes
1 answer

Usage of if constexpr in template

I am trying to understand the utility of if constexpr and want to know if there is any utility in using it in this way. template int fun() { if constexpr (B) return 1; return 0; } Is this function changed at all by using if…
jmcd
  • 15
  • 5
0
votes
1 answer

Infer type information inside if-constexpr

I want to explore the feature of if-constexpr and try to figure out type information at compile-time. For this purpose, I write the following code. I expect that printTypeInfo function will return 4 for x and 3.24 for y. However, it gives me 3.1 and…
Wang
  • 13
  • 3
0
votes
1 answer

How to return std::array of different size based on compile-time switch?

I need to create an array of static data, where the size (and data) is known at compile time, but differs between build configurations. This is a very dumbed-down version of what I'm trying to do (Please ignore glaring bad practices in this code as…
Avi Shukron
  • 6,088
  • 8
  • 50
  • 84
0
votes
1 answer

How to prevent the compiler from checking the syntactic correctness of a certain branch

I basically want to select one of the branches at compile-time but can't figure out how to address the error that shows up. Here is the code (link): #include #include #include template struct…
digito_evo
  • 3,216
  • 2
  • 14
  • 42
0
votes
0 answers

Branch with if constexpr() on constinit variables?

In the following code I try to constexpr construct a data structure and then use one of the constexpr initialized members (a_) in a method to branch with if constexpr(). From a logical standpoint AFAIK nothing speaks against it: Of course, the…
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
1 answer

C++ non-literal type error with if constexpr

Setup I defined two template classes ContainerA and ContainerB as shown here template class ContainerA{ public: constexpr explicit ContainerA(T... data) : data_(data...) {} constexpr explicit ContainerA(std::tuple
fakl
  • 127
  • 7
0
votes
2 answers

How to enable template parameter if optional function parameter is set?

I have a parsing function that I want to branch based on whether a length option is set or not. If the length option is set, the function should always check if the decreased length equals to 0. If not, it only checks for null termination. Based on…
0
votes
2 answers

C++ 17 What is the difference between a fold expression and if constexpr when using variadic template arguments (parameter pack)

I am trying to understand the difference between two approaches for handling parameter packs / variadic template arguments. There are two approaches mentioned in the linked article below. fold expression if constexpr (I don't know if this has a…
0
votes
1 answer

Generate constexpr array (error: the value of 'sum' is not usable in a constant expression)

The Problem I need to generate all possible partitions of an integer m into the sum of j elements a_k, where each a_k can be -1, 0, or 1. This is a deterministic algorithm and as such it should be able to implement it at compile time. I would like…
infinitezero
  • 1,610
  • 3
  • 14
  • 29
0
votes
1 answer

How to match wchar_t[] with constexpr in a template function?

I have a function like : template inline std::string jq2StlString(const Arg& arg) { if constexpr (std::is_same_v) { jqWideChar2StlString(arg); } else if constexpr…
tonyc
  • 41
  • 6