constexpr

using boost math constants in constexpr

早过忘川 提交于 2019-12-06 02:31:11
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. 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/boost/math/constants/constants.hpp:248:52: note: expanded from macro 'BOOST_DEFINE_MATH_CONSTANT' namespace

Is the initialization order of constexpr (i.e. constant-initialized) template variables guaranteed?

爱⌒轻易说出口 提交于 2019-12-06 01:52:45
问题 From en.cppreference.com/w/cpp/language/initialization: Unordered dynamic initialization, which [sic] applies only to (static/thread-local) class template static data members and variable templates (since C++14) that aren't explicitly specialized. Therefore static templates appear to be vulnerable to an even worse version of The Static Initialization Order Fiasco (TSIOF) (i.e. unordered within a translation unit). Does use of constexpr remove this vulnerability? i.e. is the output of the

Filling a std::array at compile time and possible undefined behaviour with const_cast

只谈情不闲聊 提交于 2019-12-06 00:47:45
问题 It is known that std::array::operator[] since C++14 is constexpr , see declaration below: constexpr const_reference operator[]( size_type pos ) const; However, it is also const qualified. This causes implications if you want to use the subscript operator of a std::array in order to assign values to your array at compile time. For example consider the following user literal: template<typename T, int N> struct FooLiteral { std::array<T, N> arr; constexpr FooLiteral() : arr {} { for(int i(0); i

Array Initialisation Compile Time - Constexpr Sequence

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 21:15:10
问题 I was reading this question on SO. The question itself is not so interesting, but I was wondering whether it exists and how to implement a compile time solution. Regard to the first sequence: All numbers except the ones which can be divided by 3. The sequence should be something like: [1, 2, 4, 5, 7, 8, 10, 11, 13, 14, ...] By induction, I've found the math formula for that sequence: f(0) = 0; f(x > 0) = floor[(3x - 1) / 2]; So I've implemented a C++ constexpr function which generates the i

C++14: Initializing constexpr variables from parameter values

☆樱花仙子☆ 提交于 2019-12-05 19:30:28
问题 Say I have a class that that can return a constant expression through a constexpr function: template<int N> struct Foo { constexpr int Bar() const { return N; } }; If I wanted to initialize constexpr values from Foo::Bar() , how should I pass a parameter of type Foo ? I've tried these two, with an example constexpr variable inside of each to test that it can be initialized: template<int N> constexpr int ByValue(Foo<N> f) { constexpr int i = f.Bar(); return f.Bar(); } template<int N> constexpr

Different behavior observed with constexpr auto/char-array variable

≡放荡痞女 提交于 2019-12-05 18:43:51
Following up with this question Having a constexpr static string gives a linker error In the question, this code wasn't able to compile: #include <iostream> struct Test { static constexpr char text[] = "Text"; }; int main() { std::cout << Test::text << std::endl; // error: undefined reference to `Test::text' } From the comment, this code is able to compile: #include <iostream> struct Test { static constexpr auto text = "Text"; }; int main() { std::cout << Test::text << std::endl; } My question is why the auto version works but the array of char version doesn't? Could you please point out the

Is the constexpr specifier required on the declaration of a constexpr static member initialized outside of the class?

六眼飞鱼酱① 提交于 2019-12-05 17:21:19
问题 C++17 §10.1.5/1 states: The constexpr specifier shall be applied only to the definition of a variable or variable template or the declaration of a function or function template. A function or static data member declared with the constexpr specifier is implicitly an inline function or variable (10.1.6). If any declaration of a function or function template has a constexpr specifier, then all its declarations shall contain the constexpr specifier. A similar paragraph has existed in the standard

Taylor series expansion as constexpr

こ雲淡風輕ζ 提交于 2019-12-05 15:49:55
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 trivial and also constexpr . I'm calling sine from within a loop like this: template <int N> void test

“surprising” constant initialization because of definition order

折月煮酒 提交于 2019-12-05 13:09:30
问题 When reading the slides about constexpr the introduction is about "surprisingly dynamic initialization with consts" . The example is struct S { static const int c; }; const int d = 10 * S::c; const int S::c = 5; Alas, the audio track is missing, so are the notes, so I can only guess what is meant here. Is it corrrect that d is "surprisingly" initialized dynamically, because S::c is defined before d ? That the declaration of S::c is before d is probably not enough, the compiler needs the

Constexpr find implementation

≯℡__Kan透↙ 提交于 2019-12-05 12:16:12
After answering this question and reading this talk and looking at this code , I want to implement constexpr find with just simple array class. Consider following example: #include <cstddef> template <class It, class T> constexpr auto constexpr_find(const It& b, const It& e, T value) { auto begin = b; while (begin != e) { if (*begin == value) break; ++begin; } return *begin; } template<typename T, size_t N> class array { public: typedef T* iterator; typedef const T* const_iterator; constexpr auto begin() const { return const_iterator(array_); } constexpr auto end() const { return const