constant-expression

Is assert usable in constant expressions?

六月ゝ 毕业季﹏ 提交于 2019-12-05 00:21:33
The assert -macro from <cassert> provides a concise way of ensuring that a condition is met. If the argument evaluates to true , it shall not have any further effects. However, can its invocation also be used inside a constant expression in that case? This was dealt with by LWG 2234 , which was brought back to attention after relaxed constraints on constexpr functions had been introduced. Proposed resolution : This wording is relative to N3936. Introduce the following new definition to the existing list in 17.3 [definitions]: constant subexpression [defns.const.subexpr] an expression whose

Confusion about constant expressions

最后都变了- 提交于 2019-12-04 17:31:48
问题 This is some kind of follow-up for this topic and deals about a little part of it. As with the previous topic, let's consider that our compiler has constexpr functions for std::initializer_list and std::array . Now, let's go straight to the point. This works: #include <array> #include <initializer_list> int main() { constexpr std::array<int, 3> a = {{ 1, 2, 3 }}; constexpr int a0 = a[0]; constexpr int a1 = a[1]; constexpr int a2 = a[2]; constexpr std::initializer_list<int> b = { a0, a1, a2 };

Confusion about constant expressions

孤者浪人 提交于 2019-12-03 10:30:45
This is some kind of follow-up for this topic and deals about a little part of it. As with the previous topic, let's consider that our compiler has constexpr functions for std::initializer_list and std::array . Now, let's go straight to the point. This works : #include <array> #include <initializer_list> int main() { constexpr std::array<int, 3> a = {{ 1, 2, 3 }}; constexpr int a0 = a[0]; constexpr int a1 = a[1]; constexpr int a2 = a[2]; constexpr std::initializer_list<int> b = { a0, a1, a2 }; return 0; } This does not : #include <array> #include <initializer_list> int main() { constexpr std:

Dividing by zero in a constant expression

大城市里の小女人 提交于 2019-12-02 20:01:49
My toy compiler crashes if I divide by zero in a constant expression: int x = 1 / 0; Is this behaviour allowed by the C and/or C++ standards? The mere presence of 1 / 0 does not permit the compiler to crash. At most, it is permitted to assume that the expression will never be evaluated, and thus, that execution will never reach the given line. If the expression is guaranteed to be evaluated, the standard imposes no requirements on the program or compiler. Then the compiler can crash. 1 / 0 is only UB if evaluated. The C11 standard gives an explicit example of 1 / 0 being defined behavior when

Is there a correct constant-expression, in terms of a float, for its msb?

这一生的挚爱 提交于 2019-12-02 03:19:18
问题 The problem: given a floating point constant expression, can we write a macro that evaluates to a constant expression whose value is a power of two equal to the most significant place of the significand? Equivalently, this is just the greatest power of two less than or equal to the input in magnitude. For the purposes of this question we can ignore: Near-overflow or near-underflow values (they can be handled with finitely many applications of ?: to rescale). Negative inputs (they can be

Is a glvalue integral constant expression a constant expression?

耗尽温柔 提交于 2019-12-01 21:04:29
N4527 5.20 [expr.const]p3 An integral constant expression is an expression of integral or unscoped enumeration type, implicitly converted to a prvalue, where the converted expression is a core constant expression. 5.20 [expr.const]p5 A constant expression is either a glvalue core constant expression whose value refers to an entity that is a permitted result of a constant expression (as defined below), or a prvalue core constant expression whose value is an object where, for that object and its subobjects: (5.1) — each non-static data member of reference type refers to an entity that is a

c#: what is a constant expression?

假如想象 提交于 2019-12-01 18:45:10
问题 I'm working with attributes at the moment. I often run into the error 'An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type.' I don't really know what 'constant expression' means. It would have been really useful to be able to pass in a Func<MyType, bool> to the attribute (to be consumed by the code which executes when the attribute is present). But alas, no. I don't understand why that type cannot be placed in

Why “initializer element is not a constant” is… not working anymore?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 06:07:43
static const int a = 42; static const int b = a; I would expect a compilation error in such code. The initializer must be a constant expression or a string literal. A value stored in an object with the type int with const type qualifier is not a constant expression. I compile with -Wall -Wextra -pedantic , even with -ansi . Then: the code compiles fine on gcc8.2 and gcc 8.1 the code fails to compile on gcc lower then 7.4 with error: initializer element is not constant the code compiles fine on all clang versions Surprisingly, the following: static const char * const a = "a"; static const char

Why “initializer element is not a constant” is… not working anymore?

风流意气都作罢 提交于 2019-12-01 04:18:45
问题 static const int a = 42; static const int b = a; I would expect a compilation error in such code. The initializer must be a constant expression or a string literal. A value stored in an object with the type int with const type qualifier is not a constant expression. I compile with -Wall -Wextra -pedantic , even with -ansi . Then: the code compiles fine on gcc8.2 and gcc 8.1 the code fails to compile on gcc lower then 7.4 with error: initializer element is not constant the code compiles fine

int a=1, is a || 1 a constant expression?

一曲冷凌霜 提交于 2019-12-01 02:05:15
N4527 5.20[expr.const]p5 A constant expression is either a glvalue core constant expression whose value refers to an entity that is a permitted result of a constant expression (as defined below), or a prvalue core constant expression whose value is an object where, for that object and its subobjects: — each non-static data member of reference type refers to an entity that is a permitted result of a constant expression, and — if the object or subobject is of pointer type, it contains the address of an object with static storage duration, the address past the end of such an object (5.7), the