constexpr

Difference between const and constexpr arrays

送分小仙女□ 提交于 2019-12-22 03:44:11
问题 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

std::is_constant_evaluated behavior

我们两清 提交于 2019-12-22 01:28:13
问题 GCC9 already implements std::is_constant_evaluated . I played a little bit with it, and I realized it is somewhat tricky. Here’s my test: constexpr int Fn1() { if constexpr (std::is_constant_evaluated()) return 0; else return 1; } constexpr int Fn2() { if (std::is_constant_evaluated()) return 0; else return 1; } int main() { constexpr int test1 = Fn1(); // Evaluates to 0 int test2 = Fn1(); // Evaluates to 0 int const test3 = Fn1(); // Evaluates to 0 constexpr int test4 = Fn2(); // Evaluates

Can't a class have static constexpr member instances of itself?

孤者浪人 提交于 2019-12-22 01:23:10
问题 This code is giving me incomplete type error. What is the problem? Isn't allowed for a class to have static member instances of itself? Is there a way to achieve the same result? struct Size { const unsigned int width; const unsigned int height; static constexpr Size big = { 480, 240 }; static constexpr Size small = { 210, 170 }; private: Size( ) = default; }; 回答1: Is there a way to achieve the same result? By "the same result", do you specifically intend the constexpr -ness of Size::big and

Why disallow goto in constexpr functions?

限于喜欢 提交于 2019-12-21 06:50:23
问题 C++14 has rules for what you can and can't do in a constexpr function. Some of them (no asm , no static variables) seem pretty reasonable. But the Standard also disallows goto in constexpr functions, even while it allows other control flow mechanisms. What's the reasoning behind this distinction? I thought we were past " goto is hard for compilers ". 回答1: My understanding is there was a desire to get relaxed constexpr semantics in C++14. A lot of the restrictions that were relaxed were

constexpr depth limit with clang (fconstexpr-depth doesnt seem to work)

和自甴很熟 提交于 2019-12-21 04:21:16
问题 Is there anyway to configure constexpr instantiation depth? I am running with -fconstexpr-depth=4096 (using clang/XCode). But still fail to compile this code with error: Constexpr variable fib_1 must be initialized by a constant expression. The code fails irrespective of whether option -fconstexpr-depth=4096 is set or not. Is this a bug with clang or is expected to behave this way. Note: this works good till fib_cxpr(26), 27 is when it starts to fail. Code: constexpr int fib_cxpr(int idx) {

constexpr depth limit with clang (fconstexpr-depth doesnt seem to work)

大憨熊 提交于 2019-12-21 04:21:02
问题 Is there anyway to configure constexpr instantiation depth? I am running with -fconstexpr-depth=4096 (using clang/XCode). But still fail to compile this code with error: Constexpr variable fib_1 must be initialized by a constant expression. The code fails irrespective of whether option -fconstexpr-depth=4096 is set or not. Is this a bug with clang or is expected to behave this way. Note: this works good till fib_cxpr(26), 27 is when it starts to fail. Code: constexpr int fib_cxpr(int idx) {

What happens when an exception is thrown while computing a constexpr?

妖精的绣舞 提交于 2019-12-21 03:33:26
问题 When computing constant expressions to initialize a constexpr it is possible to throw exceptions. For example here is an example where the computation of a constant expression is guarded against overflow: #include <iostream> #include <stdexcept> constexpr int g(int n, int n0, int n1) { return n == 0? n1: g(n - 1, n1, n0 + n1); } constexpr int f(int n) { return n < 42? g(n, 0, 1): throw std::out_of_range("too big"); } int main() { try { constexpr int f41 = f(41); // OK: constexpr int f43 = f

Why is calling a constexpr function with a member array not a constant expression?

喜夏-厌秋 提交于 2019-12-21 03:13:06
问题 I have the following helper function: template<typename T, std::size_t N> constexpr std::size_t Length(const T(&)[N]) { return N; } Which returns the length of a static array. In the past this always has worked but when I do this: struct Foo { unsigned int temp1[3]; void Bar() { constexpr std::size_t t = Length(temp1); // Error here } }; I get an error when using MSVS 2017: error C2131: expression did not evaluate to a constant note: failure was caused by a read of a variable outside its

Access to constexpr variable inside lambda expression without capturing

孤街醉人 提交于 2019-12-21 03:10:50
问题 In the following example, I can access the constexpr variable x from inside the lambda y without explicitly capturing it. This is not possible if x is not declared as constexpr . Are there special rules that apply to constexpr for capturing? int foo(auto l) { // OK constexpr auto x = l(); auto y = []{return x;}; return y(); // NOK // auto x2 = l(); // auto y2 = []{ return x2; }; // return y2(); } auto l2 = []{return 3;}; int main() { foo(l2); } 回答1: Are there special rules that apply to

Access to constexpr variable inside lambda expression without capturing

吃可爱长大的小学妹 提交于 2019-12-21 03:10:13
问题 In the following example, I can access the constexpr variable x from inside the lambda y without explicitly capturing it. This is not possible if x is not declared as constexpr . Are there special rules that apply to constexpr for capturing? int foo(auto l) { // OK constexpr auto x = l(); auto y = []{return x;}; return y(); // NOK // auto x2 = l(); // auto y2 = []{ return x2; }; // return y2(); } auto l2 = []{return 3;}; int main() { foo(l2); } 回答1: Are there special rules that apply to