constexpr

Floating point division by zero not constexpr

邮差的信 提交于 2019-12-12 11:51:56
问题 When compiling this: constexpr double x {123.0}; constexpr double y = x / 0.0; std::cout << x << " / 0 = " << y << "\n"; The compiler (gcc 4.9.2, -std=c++11 or c++14) fails, giving error: (1.23e+2 / 0.0)' is not a constant expression constexpr double y = x / 0.0; How is the result (Inf) relevant when deciding if y can be a constexpr or not? For reference, this seems to be the way to do it: static constexpr double z = std::numeric_limits<double>::quiet_NaN(); static constexpr double w = std:

When does a static constexpr class member need an out-of-class definition?

纵然是瞬间 提交于 2019-12-12 10:38:39
问题 I have the following C++11 code (simplified version): struct Info { const char * name; int version; }; class Base { public: const Info info; Base (Info info) : info (info) {} }; class Derived : public Base { public: static constexpr Info info = {"Foobar", 2}; Derived () : Base (info) {} }; int main () { static Derived derived; return 0; } GCC 4.9.1 compiles and links this code fine. Clang 3.5.0, on the other hand, complains about an undefined reference: /tmp/test-109c5c.o: In function `main':

Why is `std::invoke` not constexpr?

只愿长相守 提交于 2019-12-12 10:34:23
问题 Shouldn't std::invoke be constexpr especially after constexpr lambdas in C++17? Are there any obstacles that will prevent this? 回答1: Update: P1065 will make it constexpr . Keep original post for historical reason: From the proposal: Although there is possibility to implement standard conforming invoke function template as a constexpr function, the proposed wording does not require such implementation. The main reason is to left it consistent with existing standard function objects, that could

clang vs gcc CRTP: constexpr variable cannot have non-literal type

亡梦爱人 提交于 2019-12-12 09:46:32
问题 I have a CRTP template class here: template <typename S> class Base { public: constexpr static S NOT_SET{0}; }; struct Derived : public Base<Derived> { }; Clang (5.0.0) does not accept this: 5 : <source>:5:24: error: constexpr variable cannot have non-literal type 'const Derived' constexpr static S NOT_SET{0}; ^ 8 : <source>:8:25: note: in instantiation of template class 'Base<Derived>' requested here struct Derived : public Base<Derived> ^ 5 : <source>:5:24: note: incomplete type 'const

Are there cases where constexpr should be avoided, even it it could be used?

血红的双手。 提交于 2019-12-12 08:28:27
问题 If an object is declared const , its value is guaranteed to be available only at runtime, but if it's declared constexpr , the value is guaranteed to be available both during compilation and at runtime. So if I have an object whose value is available during compilation, are there any situations where I should not declare it constexpr ? const int magicValue = 42; // Does this ever make sense // (using const instead of constexpr)? For functions, if a function can return a value computed during

why for-loop isn't a compile time expression and extended constexpr allows for-loop in a constexpr function

杀马特。学长 韩版系。学妹 提交于 2019-12-12 07:18:42
问题 I wrote code like this #include <iostream> using namespace std; constexpr int getsum(int to){ int s = 0; for(int i = 0; i < to; i++){ s += i; } return s; } int main() { constexpr int s = getsum(10); cout << s << endl; return 0; } I understand that it works because of extended constexpr. However in this question why-isnt-a-for-loop-a-compile-time-expression, the author gave his code as follow: #include <iostream> #include <tuple> #include <utility> constexpr auto multiple_return_values() {

Using constexpr method for template parameterization inside struct

爱⌒轻易说出口 提交于 2019-12-12 01:56:58
问题 This is a continuation of the problem I found and described here. Say you have a struct that contains a static constexpr function and a type alias for a std::bitset (or any type you wish to template using the result of the const expression) that looks as follows: struct ExampleStruct { static constexpr std::size_t Count() noexcept { return 3U; } using Bitset = std::bitset<Count()>; }; Visual Studio 2015 version 14.0.25029.00 Update 2 RC highlights the Count() call in red and generates the

reference with constexpr causes multiple definition error

不打扰是莪最后的温柔 提交于 2019-12-11 10:22:53
问题 I program for MCUs with CodeSourcery g++ lite which is based on gcc 4.7.2 I want to define peripheral objects which is located at certain address. So I try to use reference with the constexpr specifier. for example: typedef int& int_ref; constexpr int_ref i = *(int*)0; If I put that code into a header, and compile my program, I will get diagnosis like: xx1.o:(.rodata.i+0x0): multiple definition of `i' ... xxx.o:(.rodata.i+0x0): first defined here collect2.exe: error: ld returned 1 exit status

How to define a static constexpr matrix in c++14?

牧云@^-^@ 提交于 2019-12-11 07:48:53
问题 I am currently using C++14. I would like to define a Matrix class which I can use for defining runtime matrices, but also constexpr matrices. I also would like to define static constexpr matrices based on such a class. I consider this as a starting point for the Matrix class. Then I would like to write something as: static constexpr Matrix<double,2,2> staticmat{0.1,0.2,0.3,0.4}; so that staticmat is constexpr and unique, being static. However, in order to initialise this, I would need a

How to build a custom macro that behaves differently when used as constexpr (like assert)?

旧城冷巷雨未停 提交于 2019-12-11 06:19:12
问题 Starting in C++14, the assert macro can be used in functions even when they are defined as constexpr. I know this has to do with the fact that it evaluates to "true", but I'm having trouble figuring out what the actual code looks like. Specifically, how do you build a macro that prints something when run in a constexpr function that is being evaluated at run time, but shuts this non-constexpr behavior off when in a constexpr function that is being evaluated at compile time. 来源: https:/