constant-expression

`static constexpr` function called in a constant expression is…an error?

偶尔善良 提交于 2019-12-17 19:08:39
问题 I have the following code: class MyClass { static constexpr bool foo() { return true; } void bar() noexcept(foo()) { } }; I would expect that since foo() is a static constexpr function, and since it's defined before bar is declared, this would be perfectly acceptable. However, g++ gives me the following error: error: ‘static constexpr bool MyClass::foo()’ called in a constant expression This is...less than helpful, since the ability to call a function in a constant expression is the entire

Conditional-Operator in Constant Expression

末鹿安然 提交于 2019-12-14 01:23:50
问题 I tried the following code snippet with MSVC 10, where it works fine. enum { FOO = (sizeof(void*) == 8 ? 10 : 20) }; int main() { return FOO; } What I would like to know is: Does the C++ Standard (preferably C++98) allow me to use the conditional-operator in a constant expression when all operands are constant expressions, or is this a Microsoft quirk/extension? 回答1: This is perfectly valid and sensible standard C++. The ternary conditional operator forms an expression , and the expression is

`static constexpr` function called in a constant expression is…an error?

狂风中的少年 提交于 2019-12-13 15:42:54
问题 I have the following code: class MyClass { static constexpr bool foo() { return true; } void bar() noexcept(foo()) { } }; I would expect that since foo() is a static constexpr function, and since it's defined before bar is declared, this would be perfectly acceptable. However, g++ gives me the following error: error: ‘static constexpr bool MyClass::foo()’ called in a constant expression This is...less than helpful, since the ability to call a function in a constant expression is the entire

C11 and constant expression evaluation in switch-case labels

我与影子孤独终老i 提交于 2019-12-13 10:06:44
问题 following this question Why doesn't gcc allow a const int as a case expression?, basically the same as What promoted types are used for switch-case expression comparison? or Is there any way to use a constant array with constant index as switch case label in C?. From the first link, I tried to replace : case FOO: // aka 'const int FOO = 10' with : case ((int) "toto"[0]): // can't be anything *but* constant Which gives : https://ideone.com/n1bmIb -> https://ideone.com/4aOSXR = works in C++

Cannot create list literal in F#

懵懂的女人 提交于 2019-12-12 12:02:05
问题 I have the following types type StatusCode = | OK = 200 | NoContent = 204 | MovedTemp = 301 | MovedPerm = 302 | SeeOther = 303 | NotModified = 304 | NotFound = 404 | ServerError = 500 [<Literal>] let NoBodyAllowedStatusCodes = [StatusCode.NoContent; StatusCode.NotModified] And I'm getting a compile-time error that says: This is not a valid constant expression or custom attribute value I can't really figure out what's wrong here. 回答1: In F#, and .NET in general, lists cannot be literals

cannot appear in a constant expression

孤街浪徒 提交于 2019-12-11 14:55:55
问题 In the following c++ programm: class matrix { public: int n; double **x; matrix(int n) : n(n) { x=new double[n][n]; for (int i=0;i<n;i++) { for(int j=0;j<n;j++) { x[i][j]=0; } } } ... I get the following error: "'n' cannot appear in a constant-expression". Since im relatively new to cpp i dont really know why this error occurs (especially because i did almost the exact same thing with a class called vector and there it was no problem at all) and how to fix it. I would really appreciate any

Java constant expressions and code elimination

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 02:08:01
问题 As discussed here, javac and other Java compilers may provide code elimination capabilities for if -statements where the condition is a "Constant Expression". How is this affected if my code uses a constant expression that depends on other constant expressions defined in different packages? For example, let's say I have the following classes in the respective specified packages: package foo; public class Foo { public static final boolean CONDITION = false; } and package bar; import foo.Foo;

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

有些话、适合烂在心里 提交于 2019-12-09 02:06:54
问题 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

Typo at msdn page “C++ Constant Expressions”?

我的未来我决定 提交于 2019-12-07 12:39:53
问题 It says at msdn page for c++ constant expressions that: Nonintegral constants must be converted (either explicitly or implicitly) to integral types to be legal in a constant expression. Therefore, the following code is legal: const double Size = 11.0; char chArray[(int)Size]; At least on VC++ 10.0 the second line produces: "error C2057: expected constant expression". So is it legal on some other compiler or is the msdn page simply wrong? 回答1: According to 5.19/1 : An integral constant

Parameterized tests in f# - This is not a valid constant expression

巧了我就是萌 提交于 2019-12-07 05:02:12
问题 For some reason when passing arguments to the test via TestCase attrubute, I get the following error message about the first argument, which in this case is an array: This is not a valid constant expression or custom attribute value module GameLogicTest = open FsUnit open NUnit.Framework open GameLogic.Examle // This is not a valid constant expression or custom attribute value [<TestCase( [| 1; 2; 3 |], 3, 1,1)>] let ``let example.`` (a, m, h, c) = a |> proof1 m |> should equal (h,c) But when