if-constexpr

Equivalent ternary operator for constexpr if?

孤者浪人 提交于 2019-12-04 15:15:37
问题 Maybe I missed something, but I can't find any hints: is there a constexpr ternary operator in C++17 equivalent to constexpr-if? template<typename Mode> class BusAddress { public: explicit constexpr BusAddress(Address device) : mAddress(Mode::write ? (device.mDevice << 1) : (device.mDevice << 1) | 0x01) {} private: uint8_t mAddress = 0; }; 回答1: No, there is no constexepr conditional operator. But you could wrap the whole thing in a lambda and immediately evaluate it (an IIFE): template

Why doesn't this “undefined extern variable” result in a linker error in C++17?

对着背影说爱祢 提交于 2019-12-04 08:47:57
问题 I have compiled and ran the following program in a C++17 compiler (Coliru). In the program, I declared an extern variable, but did not define it. However, the compiler doesn't give a linker error . #include <iostream> extern int i; // Only declaration int func() { if constexpr (true) return 0; else if (i) return i; else return -1; } int main() { int ret = func(); std::cout<<"Ret : "<<ret<<std::endl; } Why doesn't the compiler give a linker error? 回答1: Because the variable isn't odr-used. You

Equivalent ternary operator for constexpr if?

↘锁芯ラ 提交于 2019-12-03 09:31:06
Maybe I missed something, but I can't find any hints: is there a constexpr ternary operator in C++17 equivalent to constexpr-if? template<typename Mode> class BusAddress { public: explicit constexpr BusAddress(Address device) : mAddress(Mode::write ? (device.mDevice << 1) : (device.mDevice << 1) | 0x01) {} private: uint8_t mAddress = 0; }; No, there is no constexepr conditional operator. But you could wrap the whole thing in a lambda and immediately evaluate it (an IIFE ): template<typename Mode> class BusAddress { public: explicit constexpr BusAddress(Address device) : mAddress([&]{ if

Why doesn't this “undefined extern variable” result in a linker error in C++17?

删除回忆录丶 提交于 2019-12-03 00:58:49
I have compiled and ran the following program in a C++17 compiler (Coliru). In the program, I declared an extern variable, but did not define it. However, the compiler doesn't give a linker error . #include <iostream> extern int i; // Only declaration int func() { if constexpr (true) return 0; else if (i) return i; else return -1; } int main() { int ret = func(); std::cout<<"Ret : "<<ret<<std::endl; } Why doesn't the compiler give a linker error? Because the variable isn't odr-used. You have a constexpr if there that always discards the branch that could use it. One of the points of constexpr

Comparing constexpr function parameter in constexpr-if condition causes error

孤者浪人 提交于 2019-12-02 04:09:00
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 I replace test_int with a different function: constexpr bool test_int_no_if(const int i) { return (i =

if constexpr(condition) as compile-time conditional

拥有回忆 提交于 2019-12-01 20:02:48
I want to use a constexpr bool ( useF in the example below) to enable a feature in the following code. Here, calling A::f() . Additionally, I want to be the alias-template ( a ) to be void in the case I switch off the feature. I tried to use a constexpr if statement, but the body is still being instantiated, which causes a compile error. If I use a wrapper template ( X ), the body is being discarded as I'd expected, but that seems ugly to me. Are there any other ways to do this? constexpr bool useF = false; struct A { static void f() {} }; using a = std::conditional<useF, A, void>::type;

if constexpr(condition) as compile-time conditional

不想你离开。 提交于 2019-12-01 19:50:06
问题 I want to use a constexpr bool ( useF in the example below) to enable a feature in the following code. Here, calling A::f() . Additionally, I want to be the alias-template ( a ) to be void in the case I switch off the feature. I tried to use a constexpr if statement, but the body is still being instantiated, which causes a compile error. If I use a wrapper template ( X ), the body is being discarded as I'd expected, but that seems ugly to me. Are there any other ways to do this? constexpr

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

吃可爱长大的小学妹 提交于 2019-11-27 19:40:26
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 check should be left unpreformed on a discarded branch. Making an indirection via a constexpr lambda does

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

最后都变了- 提交于 2019-11-27 08:48:55
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 (condition) { return 0; } else { // optimized out even without "constexpr if" return 1; } } Godbolt

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

ε祈祈猫儿з 提交于 2019-11-27 05:15:34
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 " << value << std::endl; } int main() { auto n = 1000; print(n); print(&n); } But when I rewrite the above