constexpr

constexpr does not work/apply inside function call

廉价感情. 提交于 2019-12-23 03:25:04
问题 I have implemented a constexpr compile-time hash-function, which works fine (i.e. is evaluated at compile-time) if called as constexpr auto hash = CompileTimeHash( "aha" ); but I need to use it in actual code as an argument to a function as in foo( CompileTimeHash( "aha" ) ); // foo is NOT constexpr For a specific reason, I cannot use the long version constexpr auto hash = CompileTimeHash( "aha" ); foo( hash ); The compiler (VC++) will not compile-time hash in the short (first) case. Is there

using boost math constants in constexpr

…衆ロ難τιáo~ 提交于 2019-12-22 10:57:23
问题 Is is possible to use boost math constants in constexpr? For example, the following line: static constexpr double SEC3 = static_cast<double>(45)/180*boost::math::double_constants::pi; gives me error Error - constexpr variable 'SEC3' must be initialized by a constant expression But if I replace the boost code with simple M_PI, it works fine. 回答1: I suspect this may be the reason. Coliru gives this error: clang++ -std=c++1y -O2 -Wall -pedantic -pthread main.cpp && ./a.out /usr/local/include

using boost math constants in constexpr

我只是一个虾纸丫 提交于 2019-12-22 10:56:00
问题 Is is possible to use boost math constants in constexpr? For example, the following line: static constexpr double SEC3 = static_cast<double>(45)/180*boost::math::double_constants::pi; gives me error Error - constexpr variable 'SEC3' must be initialized by a constant expression But if I replace the boost code with simple M_PI, it works fine. 回答1: I suspect this may be the reason. Coliru gives this error: clang++ -std=c++1y -O2 -Wall -pedantic -pthread main.cpp && ./a.out /usr/local/include

Conditionally constexpr member function

岁酱吖の 提交于 2019-12-22 08:57:30
问题 Suppose I have a template class template <typename T> class foo { T m; decltype(auto) f() { return m.f(); } }; How can I give foo:f() the constexpr specifier only if T::f() is constexpr? 回答1: You just slap a constexpr on it: constexpr decltype(auto) f() { return m.f(); } Yes, it's perfectly still valid even if T::f() isn't constexpr ; such a function simply can't be used in constant expressions. See [dcl.constexpr]/6. 来源: https://stackoverflow.com/questions/41517603/conditionally-constexpr

Taylor series expansion as constexpr

一个人想着一个人 提交于 2019-12-22 08:47:33
问题 I'm trying to build a simple sine function using taylor series expansion that can be evaluated at compile time using C++14 constexpr . My code is compiling, but the compiler doesn't generate a constant. sine is defined as follows: template <int P, typename T = double> constexpr T sine(T x) { T result = x; for (int i = 1; i < P; ++i) result += power<T>(-1, i) * power<T>(x, 1 + 2 * i) / factorial<T>(1 + 2 * i); return result; } I can provide code for power and factorial if needed. They are

Undefined reference to static constexpr string (except if it's a pointer)

纵然是瞬间 提交于 2019-12-22 08:35:38
问题 This work: template<typename T> struct Something { static constexpr const char* str = "int"; }; int main() { std::cout << Something<int>::str << std::endl; } But it doesn't: template<typename T> struct Something { static constexpr const char str[] = "int"; }; int main() { std::cout << Something<int>::str << std::endl; } gcc-4.8 says: "undefined reference to Something<int>::str ". This error can be solved defining the static member outside of the class: template<typename T> constexpr const

Initializing c++ std::bitset at compile time

旧时模样 提交于 2019-12-22 07:55:58
问题 I'm trying to initialize a std::bitset<256> at compile time with some of its indices, lets say 50-75 and 200-225 set to 1. Based on http://en.cppreference.com/w/cpp/utility/bitset/bitset It looks like my 2 options are: constexpr bitset(); constexpr bitset( unsigned long long val ); Could someone shed light on how I would initialize my bitset considering the second constructor wouldn't work for large indices? 回答1: Bitset constructor constexpr bitset( unsigned long long val ) is limited in the

Is GCC correct in requiring the constexpr specifier for this reference declaration?

ε祈祈猫儿з 提交于 2019-12-22 04:46:14
问题 The code below doesn't compile under GCC 5.3.0 because the declaration of r is missing a constexpr specifier. const int i = 1; const int& r = i; constexpr int j = r; I believe the rejection is correct. How do I prove it using the working draft N4527? 回答1: First, since we're using a reference, [expr.const]/(2.9) must not be violated. (2.9.1) applies, though: an id-expression that refers to a variable or data member of reference type unless the reference has a preceding initialization and

C++: struct member in a switch statement

青春壹個敷衍的年華 提交于 2019-12-22 04:27:09
问题 I am writing a microprocessor emulator in C++, and one of my goals was to make it the code very readable. To implement opcodes, I have a struct which I am using to represent individual processor instructions, and it contains both the opcode and how far to advance the program counter. The idea was to group related information about each instruction. struct instruction { const int opcode; // instruction opcode const int op_size; // how far to advance Program Counter }; const instruction HALT

Difference between const and constexpr arrays

我们两清 提交于 2019-12-22 03:44:45
问题 Why is there a difference between const and constexpr when used with arrays? int const xs[]{1, 2, 3}; constexpr int ys[]{1, 2, 3}; int as[xs[0]]; // error. int bs[ys[0]]; // fine. I would expect both xs[0] and ys[0] to be constant expressions but only the latter is treated as such. 回答1: A longer comment as community wiki. The expression xs[0] is defined in [expr.sub]/1 as *((xs)+(0)) . (See below for the parantheses.) One of the expressions shall have the type “pointer to T ” and the other