constexpr

calling non constexpr function from constexpr allowed in some conditions

☆樱花仙子☆ 提交于 2019-12-01 18:08:05
Coming from that question: How to build a custom macro that behaves differently when used as constexpr (like assert)? I wonder why it is possible to call a non constexpr function if it is conditional. void bla( ) { std::cout << "bla called!" << std::endl; } constexpr bool check(bool condition) { //bla(); // can not directly be called -> not constexpr! condition ? void (0) : bla(); // compiles and runs even if condition is true or false! // if condition is const, it did not compile because it // directly force execution of non constexpr function true ? void(0): bla(); // also that compiles!, ok

Purpose of constexpr

萝らか妹 提交于 2019-12-01 17:48:22
问题 This is more of a philosophical question rather than practical code snippet, but perhaps C++ gurus can enlighten me (and apologies if it's been asked already). I have been reading Item 15 in Meyers's "Effective Modern C++" book, as well as this thread: implicit constexpr? (plus a reasonable amount of googling). The item goes over usage of constexpr for expressions, namely that it defines functions that can return compile time values given compile time inputs. Moreover, the StackOverflow

calling non constexpr function from constexpr allowed in some conditions

倖福魔咒の 提交于 2019-12-01 17:35:52
问题 Coming from that question: How to build a custom macro that behaves differently when used as constexpr (like assert)? I wonder why it is possible to call a non constexpr function if it is conditional. void bla( ) { std::cout << "bla called!" << std::endl; } constexpr bool check(bool condition) { //bla(); // can not directly be called -> not constexpr! condition ? void (0) : bla(); // compiles and runs even if condition is true or false! // if condition is const, it did not compile because it

constexpr of static tuple class member has linker error

筅森魡賤 提交于 2019-12-01 17:30:51
问题 I have the following code: #include <iostream> #include <tuple> class T { public: using Names = std::tuple<char const*, char const*>; static constexpr Names names {"First", "Second"}; }; int main() { std::cout << std::get<0>(T::names); } As names is a constexpr I expected this to work. But I get a linker error: The compiler: > g++ --version Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 6.0 (clang-600.0

Using lambda captured constexpr value as an array dimension

青春壹個敷衍的年華 提交于 2019-12-01 17:29:28
问题 GCC and Clang do compile the following code: void Test() { constexpr int Size = 3; auto Lambda = [Size]{ int Dim[Size]; }; } However, VisualStudio 2015 CTP 6 does not. Nevertheless, all 3 compilers are happy with this code: void Test() { static constexpr int Size = 3; auto Lambda = []{ int Dim[Size]; }; } Which snippet is actually doing it in the correct way? What does the C++ standard say? This question is related to Lambda capturing constexpr object 回答1: C++11 [expr.prim.lambda]/12 If a

How can this code be constexpr? (std::chrono)

雨燕双飞 提交于 2019-12-01 16:56:50
In the standards paper P0092R1, Howard Hinnant wrote: template <class To, class Rep, class Period, class = enable_if_t<detail::is_duration<To>{}>> constexpr To floor(const duration<Rep, Period>& d) { To t = duration_cast<To>(d); if (t > d) --t; return t; } How can this code work? The problem is that operator-- on a std::chrono::duration is not a constexpr operation. It is defined as: duration& operator--(); And yet this code compiles, and gives the right answer at compile time: static_assert(floor<hours>(minutes{3}).count() == 0, "”); What's up with that? The answer is that not all operations

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

安稳与你 提交于 2019-12-01 16:52:35
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 Clang 3.1. The final idea was to go Alexandrescuesque and do this: template <typename T1, typename T2>

Initializing a `static constexpr double` with MSVC 2013

て烟熏妆下的殇ゞ 提交于 2019-12-01 16:36:43
Title says it all and both of the usual ways do not work. What am I missing? 1. class Cl { static constexpr double PI; }; constexpr double Cl::PI = 3.14; (26): error C2737: 'private: static double const Cl::PI' : 'constexpr' object must be initialized 2. class Cl { static constexpr double PI = 3.14; }; (26): error C2864: 'Cl::PI' : a static data member with an in-class initializer must have non-volatile const integral type type is 'const double' In both attempts, the error is on the same line inside the class. I am using the VisualStudio/MSVC Nov 2013 CTP compiler. Note that making the

Static member access in constant expressions

十年热恋 提交于 2019-12-01 16:01:18
Accessing static class member functions or variables, can be done in two ways: through an object ( obj.member_fun() or obj.member_var ) or through the class ( Class::member_fun() or Class::member_var ). However, in constexpr functions, Clang gives an error on the object access and requires to use class access: struct S { constexpr static auto s_v = 42; constexpr static auto v() { return s_v; } }; #define TEST 1 constexpr auto foo(S const& s [[maybe_unused]]) { #if TEST constexpr auto v = s.v(); // ERROR for clang, OK for gcc #else constexpr auto v = S::v(); // OK for clang and gcc #endif

Initializing a `static constexpr double` with MSVC 2013

牧云@^-^@ 提交于 2019-12-01 15:45:52
问题 Title says it all and both of the usual ways do not work. What am I missing? 1. class Cl { static constexpr double PI; }; constexpr double Cl::PI = 3.14; (26): error C2737: 'private: static double const Cl::PI' : 'constexpr' object must be initialized 2. class Cl { static constexpr double PI = 3.14; }; (26): error C2864: 'Cl::PI' : a static data member with an in-class initializer must have non-volatile const integral type type is 'const double' In both attempts, the error is on the same line