if-constexpr

Why doesn't an if constexpr make this core constant expression error disappear?

陌路散爱 提交于 2019-11-26 19:57:41
问题 In reference to this question. The core constant expression that is used to initialize the constexpr variable y is ill-formed. So much is a given. But if I try to turn the if into an if constexpr : template <typename T> void foo() { constexpr int x = -1; if constexpr (x >= 0){ constexpr int y = 1 << x; } } int main(){ foo<int>(); } The error persists. With GCC 7.2 still giving: error: right operand of shift expression '(1 << -1)' is negative [-fpermissive] But I thought that the semantic

“constexpr if” vs “if” with optimizations - why is “constexpr” needed?

有些话、适合烂在心里 提交于 2019-11-26 14:18:45
问题 C++1z will introduce "constexpr if" - an if that will have one of branches removed, based on the condition. Seems reasonable and useful. However, is it not possible to do without constexpr keyword? I think that during compilation, compiler should know wheter condition is known during compilation time or not. If it is, even the most basic optimization level should remove unnecessary branch. For example (see in godbolt: https://godbolt.org/g/IpY5y5): int test() { const bool condition = true; if

“If constexpr” in C++17 does not work in a non-templated function

孤人 提交于 2019-11-26 11:29:06
问题 I tried to play with the C++17 standard. I tried to use one of the features of C++17 if constexpr . And I had a problem... Please take a look at the following code. This compiles without errors. In the following code, I tried to use if constexpr to check if it is a pointer. #include <iostream> #include <type_traits> template <typename T> void print(T value) { if constexpr (std::is_pointer_v<decltype(value)>) std::cout << \"Ptr to \" << *value << std::endl; // Ok else std::cout << \"Ref to \"