if-constexpr

Why does the false branch of “if constexpr” get compiled?

强颜欢笑 提交于 2020-08-26 10:23:26
问题 Why is this code giving error while compiling? My knowledge (and also this) of " if constexpr " says the else block shouldn't get compiled. if constexpr (true) { int a = 10; } else { int b = 10 } The error is: error: expected ‘,’ or ‘;’ before ‘}’ token Compiler used: g++ version 7.5.0 While compiling I used -std=c++17 flag. P.S. The missing ';' is intentional, just to check whether else is being compiled or not. 回答1: There are 2 separate, but related issues here. Firstly, if constexpr will

Errors when using constexpr-if: expected '(' before 'constexpr'

风流意气都作罢 提交于 2020-01-24 11:53:32
问题 I am trying to use if-constexpr to check something, but I encounter errors like expected '(' before 'constexpr' 'else' without a previous 'if' " So far i check there is nothing wrong with my codes My compiling flag is g++ -std=c++17 main.cpp #include <iostream> template<typename T, typename Comp = std::less<T> > struct Facility { template<T ... list> struct List { static void print() { std::cout<<"\""<<"Empty List"<<"\""<<"\n"; } }; template<T head,T ... list> struct List<head,list...> {

Errors when using constexpr-if: expected '(' before 'constexpr'

爷,独闯天下 提交于 2020-01-24 11:52:36
问题 I am trying to use if-constexpr to check something, but I encounter errors like expected '(' before 'constexpr' 'else' without a previous 'if' " So far i check there is nothing wrong with my codes My compiling flag is g++ -std=c++17 main.cpp #include <iostream> template<typename T, typename Comp = std::less<T> > struct Facility { template<T ... list> struct List { static void print() { std::cout<<"\""<<"Empty List"<<"\""<<"\n"; } }; template<T head,T ... list> struct List<head,list...> {

if constexpr - why is discarded statement fully checked?

一笑奈何 提交于 2020-01-22 17:37:48
问题 I was messing around with c++20 consteval in GCC 10 and wrote this code #include <optional> #include <tuple> #include <iostream> template <std::size_t N, typename Predicate, typename Tuple> consteval std::optional<std::size_t> find_if_impl(Predicate&& pred, Tuple&& t) noexcept { constexpr std::size_t I = std::tuple_size_v<std::decay_t<decltype(t)>> - N; if constexpr (N == 0u) { return std::nullopt; } else { return pred(std::get<I>(t)) ? std::make_optional(I) : find_if_impl<N - 1u>(std:

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

淺唱寂寞╮ 提交于 2020-01-06 04:45:08
问题 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 then modified it, so they do the same The first example is using template metaprogramming: example 1 template<int N> struct NGenerator { static const int result = N + NGenerator<N-1>::result; }; template<> struct NGenerator<0> { static const int

std::is_constant_evaluated behavior

我们两清 提交于 2019-12-22 01:28:13
问题 GCC9 already implements std::is_constant_evaluated . I played a little bit with it, and I realized it is somewhat tricky. Here’s my test: constexpr int Fn1() { if constexpr (std::is_constant_evaluated()) return 0; else return 1; } constexpr int Fn2() { if (std::is_constant_evaluated()) return 0; else return 1; } int main() { constexpr int test1 = Fn1(); // Evaluates to 0 int test2 = Fn1(); // Evaluates to 0 int const test3 = Fn1(); // Evaluates to 0 constexpr int test4 = Fn2(); // Evaluates

Comparing constexpr function parameter in constexpr-if condition causes error

余生长醉 提交于 2019-12-20 05:13:50
问题 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 with the following flags: g++-7 -std=c++1z test.cpp -o test I get the following error message: test.cpp: In function 'constexpr bool test_int(int)': test.cpp:3:21: error: 'i' is not a constant expression if constexpr(i == 5) { return true; } However, if