constexpr

How do I capture the results of a recursive function at compile-time?

你。 提交于 2019-12-10 18:23:38
问题 #include <iostream> template <typename T> struct node { T value; node const* prev; constexpr node(const T& value, node const* prev = nullptr) : value{value}, prev{prev} {} constexpr node push_front(const T& value) const { return node(value, this); } }; struct Something { node<int> n; constexpr Something(const int i) : n{node<int>(i)} {} constexpr Something(const node<int>& n) : n{n} {} }; constexpr void print(const Something& s) { bool first = true; for (const node<int>* i = &s.n; i !=

Is it possible to evaluate array on compilation time?

夙愿已清 提交于 2019-12-10 18:19:28
问题 I need to store the array of first N Fibonacci numbers. const int N = 100; long long int fib[N] = {0}; fib[0] = 1; fib[1] = 1; for(int i = 2; i < N; ++i) fib[i] = fib[i-2] + fib[i-1]; return 0; Is it possible to make fib[] constexpr, and evaluate it at compilation time somehow ? 回答1: First of all you have to write Fibonacci algorithm in compile time version, so consider following: template <size_t N> struct Fibo { static constexpr const size_t value {Fibo<N-2>::value + Fibo<N-1>::value}; };

Why can't C++ automatically figure out when to use constexpr without making us write the tag? [duplicate]

巧了我就是萌 提交于 2019-12-10 17:47:25
问题 This question already has answers here : Why do we need to mark functions as constexpr? (4 answers) Closed 3 years ago . Item 15 of Scott Meyer's Modern C++ book: "use constexpr whenever possible". He says you can mark a function constexpr and still call it with values that aren't known at compile-time -- in that case it'll behave like any other runtime function, so you get the benefits of compile-time computation if possible but can still use it with non-compile time values too. The compiler

Nested `constexpr` function calls before definition in a constant-expression context

微笑、不失礼 提交于 2019-12-10 16:46:50
问题 From what I gather from this answer, a constexpr function's result is not a constant-expression if the function has not been declared yet. What surprises me is the following code snippet : constexpr int f(); constexpr int g() { return f(); } constexpr int f() { return 42; } int main() { constexpr int i = g(); return i; } This compiles without trouble and works. Moving f 's definition past main triggers error: 'constexpr int f()' used before its definition , as I would expect. I presume that

Will const and constexpr eventually be the same thing?

人盡茶涼 提交于 2019-12-10 15:24:38
问题 I just read the answer to const vs constexpr on variables and am watching this Google Tech Talk about C++11/14 features , in which it is said that, well, constexpr might not be necessary in the future when it comes to functions, since compilers will evolve to figure it out on their own. Finally, I know that Java compilers and JVMs work hard to figure out that classes (or any variable maybe) are immutable after construction - without you explicitly saying so - and doing all sorts of wicked

constexpr with untouched non-constexpr arguments: Who is correct, clang or gcc?

送分小仙女□ 提交于 2019-12-10 15:24:05
问题 I have 4 test cases and I believe that all of them are valid: constexpr int f(int const& /*unused*/){ return 1; } void g(int const& p){ constexpr int a = f(p); // clang error, gcc valid int v = 0; constexpr int b = f(v); // clang valid, gcc valid int const& r = v; constexpr int c = f(r); // clang error, gcc error int n = p; constexpr int d = f(n); // clang valid, gcc valid } int main(){ int p = 0; g(p); } Clang and GCC differ only in the first test case. I tested with clang 4 & 5 (20170319)

What C++14 rule prohibits constexpr functions from making assignments to data members?

三世轮回 提交于 2019-12-10 14:12:12
问题 My understanding is that this (nonsensical) code is not valid C++14: class Point { public: constexpr double setX(double newX) { return x = newX; } private: double x; }; I'm trying to figure out what part of the (still officially draft) C++14 Standard disallows it. The restrictions on constexpr functions are listed in 7.1.5/2. (Sorry for the mangled formatting. I can't figure out how to beat markdown into making it look right.) The definition of a constexpr function shall satisfy the following

C++ constexpr at compile time

时光怂恿深爱的人放手 提交于 2019-12-10 13:56:46
问题 Am I right to think that this function should only be evaluated at compile time, or is there a run-time cost to it? template <typename T> size_t constexpr CompID() { return typeid(T).hash_code(); } struct Foo {}; int main(int argc, const char * argv[]) { size_t foo = CompID<Foo>(); return 0; } 回答1: constexpr function allows the function to be evaluated at compile time, but does not require that, so your answer is "maybe". It depends on the compiler's optimization settings. §7.1.5[dcl

Constexpr for creating objects

流过昼夜 提交于 2019-12-10 13:55:48
问题 I'm trying to figure out if there is a performance gain of creating objects with constexpr instead of normally. Here is the code snippet for constexpr . class Rect { const int a; const float b; public: constexpr Rect(const int a,const float b) : a(a),b(b){} }; int main() { constexpr Rect rect = Rect(1,2.0f); } And without constexpr . class Rect { int a; float b; public: Rect(int a, float b) : a(a),b(b){} }; int main() { Rect rect = Rect(1,2.0f); } I was expecting there will be a lot less code

constexpr: errors with floating point representation?

倖福魔咒の 提交于 2019-12-10 13:48:25
问题 I was hoping to convert a constant from degrees to radians (at compile time), so I opted to use a constexpr. However, my program would not compile and so I tried to debug the problem with a few tests. These tests continue to produce errors during compilation. The problem appears to be correlated with floating point arithmetic when many significant digits are involved. I tried a quick google search, and I read section 10.4 (Constant Expressions) in Stroustrup's book. Any help would be greatly