constexpr

passing a static constexpr variable by universal reference?

狂风中的少年 提交于 2019-12-19 19:54:57
问题 In the following, static constexpr member L is initialized in-class A and then passed by value or by (universal) reference. The latter fails in Clang but not in GCC, and behaviour is slightly different for member/non-member functions. In more detail: #include <iostream> using namespace std; struct A { static constexpr size_t L = 4; template <typename T> void member_ref(T&& x) { cout << std::forward<T>(x) << endl; } template <typename T> void member_val(T x) { cout << x << endl; } }; template

How to check a double's bit pattern is 0x0 in a C++11 constexpr?

北慕城南 提交于 2019-12-19 16:13:20
问题 I want to check that a given double/float variable has the actual bit pattern 0x0. Don't ask why, it's used in a function in Qt ( qIsNull() ) that I'd like to be constexpr . The original code used a union: union { double d; int64_t i; } u; u.d = d; return u.i == 0; This doesn't work as a constexpr of course. The next try was with reinterpret_cast : return *reinterpret_cast<int64_t*>(&d) == 0; But while that works as a constexpr in GCC 4.7, it fails (rightfully, b/c of pointer manipulation) in

Initializing a constexpr with a const, — int vs float

做~自己de王妃 提交于 2019-12-19 12:24:35
问题 I'm wondering why the integer ii is initiallized at compile time, but not the float ff here: int main() { const int i = 1; constexpr int ii = i; const float f = 1.0; constexpr float ff = f; } This is what happens when I try to compile: > g++ -std=c++11 test.cc test.cc: In function ‘int main()’: test.cc:6:24: error: the value of ‘f’ is not usable in a constant expression constexpr float ff = f; ^ test.cc:5:15: note: ‘f’ was not declared ‘constexpr’ const float f = 1.0; 回答1: Constant variables

Initializing a constexpr with a const, — int vs float

房东的猫 提交于 2019-12-19 12:24:14
问题 I'm wondering why the integer ii is initiallized at compile time, but not the float ff here: int main() { const int i = 1; constexpr int ii = i; const float f = 1.0; constexpr float ff = f; } This is what happens when I try to compile: > g++ -std=c++11 test.cc test.cc: In function ‘int main()’: test.cc:6:24: error: the value of ‘f’ is not usable in a constant expression constexpr float ff = f; ^ test.cc:5:15: note: ‘f’ was not declared ‘constexpr’ const float f = 1.0; 回答1: Constant variables

constexpr constructor with compile time validation

只愿长相守 提交于 2019-12-19 10:08:04
问题 I'd like to build up a class with the option of constexpr-ness. And, of course, I'd like to take advantage of compile time error check. Every constexpr function, constructor included, must work also at runtime, when the given parameters are not constant expression. That's should be the reason why every time you use static_assert in a constexpr function upon a function parameter it fails to compile. Said so, I've read that one can use the exception throwing mechnanism, since when the function

Branching on constexpr evaluation / overloading on constexpr

可紊 提交于 2019-12-19 08:49:30
问题 The setup: I have a function that uses SIMD intrinsics and would like to use it inside some constexpr functions. For that, I need to make it constexpr. However, the SIMD intrinsics are not marked constexpr, and the constant evaluator of the compiler cannot handle them. I tried replacing the SIMD intrinsics with a C++ constexpr implementation that does the same thing. The function became 3.5x slower at run-time, but I was able to use it at compile-time (yay?). The problem : How can I use this

CRTP compiling error

馋奶兔 提交于 2019-12-19 08:43:44
问题 The following will compile with GCC 5.2 but not with Visual Studio 2015. template <typename Derived> struct CRTP { static constexpr int num = Derived::value + 1; }; struct A : CRTP<A> { static constexpr int value = 5; }; It complains that A does not have a member named value . How to fix the code so that it compiles on both compilers? Or is it illegal altogether? 回答1: Try making it a constexpr function instead. The way you have it setup now attempts to access an incomplete type. Since a

Why GCC does not evaluate constexpr at compile time?

杀马特。学长 韩版系。学妹 提交于 2019-12-19 08:21:32
问题 As an example: class something { public: static constexpr int seconds(int hour, int min, int sec) { return hour*3600+min*60+sec; } } then: printf("Look at the time: %d\n", something::seconds(10, 0, 0)); Will compile to a call to the function using g++, instead of putting a constant number. Why would g++ do that? There's no gain in it and kinda defeats the purpose of using constexpr instead of awful macros. 回答1: Why would g++ do that? constexpr functions only must be evaluated at compile time

Compiler error when initializing constexpr static class member

可紊 提交于 2019-12-19 06:39:21
问题 I've declared a class in the following way class A { struct B { constexpr B(uint8_t _a, uint8_t _b) : a(_a), b(_b) {} bool operator==(const B& rhs) const { if((a == rhs.a)&& (b == rhs.b)) { return true; } return false; } uint8_t a; uint8_t b; }; constexpr static B b {B(0x00, 0x00)}; }; But g++ says error: field initializer is not constant Can't figure out where I'm wrong. 回答1: Clang is more helpful: 27 : error: constexpr variable 'b' must be initialized by a constant expression constexpr

C++0x error with constexpr and returning template function

白昼怎懂夜的黑 提交于 2019-12-19 05:48:35
问题 I tried to find a solution for the problem of the question C++ template non-type parameter type deduction, which does not involve a template parameter to call f, but implicitly chooses the correct type for the template parameter. Since constexpr should guarantee that a function only contains compile time constants, and is evaluated at compile time (at least thats what i think it does), i thought it might be the solution for this issue. So i came up with this: template <class T, T VALUE> void