Questions tagged [constant-expression]

Constant expressions can be evaluated at compile time.

Many languages require certain initializers to be constant expressions. For example:

  • Array bounds
  • Selectors in case statements
  • Bit-field length specification
  • Enumeration initializers
  • Non-type template arguments

A greatly restricted set of operands are allowed in constant expressions. Generally, variables are not allowed. For example, C++ allows:

  • Literals
  • Enumeration constants
  • Values declared as const that are initialized with constant expressions
  • sizeof expressions

Questions with this tag usually need help with error messages that indicate lines that require constant expressions.

215 questions
3
votes
1 answer

How to get a compile time error in constant evaluated expression?

I have an Assert function that I use to evaluate assertion: if the precondition fails at runtime, this function will output an error message and it will terminate the program. if the precondition fails inside a constant expression, it will cause a…
Oliv
  • 17,610
  • 1
  • 29
  • 72
3
votes
1 answer

Use of String.join in Constant Expression

I have a very long String constant which is used for an annotation, and this string is essentially a comma separated list. What I would like to be able to do is the following: String str = String.join(", ", "abc", "def", "ghi",…
Samuel Barr
  • 434
  • 4
  • 12
3
votes
2 answers

C++ integral constant expression definition

In the current C++ standard there is the following paragraph (expr.const#5) (emphasis mine): An integral constant expression is an expression of integral or unscoped enumeration type, implicitly converted to a prvalue, where the converted…
user42768
  • 1,951
  • 11
  • 22
3
votes
5 answers

Address of static const isn't const expression?

I though address-of-static was a constant expression as in the example below but I get a compiler error (or is this new to C++0x?) class X { static const int x; enum { y = &x }; };
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
3
votes
3 answers

How to set array size with const member variable?

I tried to set array size with const member variable like below class A { const int SIZE; A() : SIZE(10) {} void aaa() { int a[SIZE]; } }; I cannot build a[SIZE] like this expression. When I use GCC, build success. But when I use VC++…
Dayamre
  • 1,907
  • 2
  • 13
  • 10
3
votes
2 answers

Typo at msdn page "C++ Constant Expressions"?

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…
zeroes00
  • 531
  • 3
  • 16
3
votes
2 answers

Templates, arrays and constant expressions

Consider the code below: template struct S { }; template constexpr auto f(const char (&ref) [N]) { return S{}; } int main() { constexpr auto v = f("foo"); (void)v; } It doesn't compile for ref[0] is not a…
skypjack
  • 49,335
  • 19
  • 95
  • 187
3
votes
2 answers

Minimum Guaranteed Constant Folding in C

Question I am curious if there is any guarantees about constant folding being done in C. Where I've looked This link on a site whose reputability is unknown to me makes an offhand comment: All C compilers can fold integer constant expressions that…
mtraceur
  • 3,254
  • 24
  • 33
3
votes
1 answer

Is a glvalue integral constant expression a constant expression?

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…
stackcpp
  • 1,275
  • 7
  • 15
3
votes
2 answers

Initialize C++ array2 using constant array1 known at compile time

I have the following array: int const A[4] = { 0, 1, 2, 3 }; I want to initialize a duplicate array, as follows: int a[4] = A; If I run g++ 4.8.2 on cygwin as follows: g++ --std=c++11 myfile.cpp I get the following error: myfile.cpp:16:16: error:…
0xbe5077ed
  • 4,565
  • 6
  • 35
  • 77
3
votes
1 answer

Conditional-Operator in Constant Expression

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…
mooware
  • 1,722
  • 2
  • 16
  • 25
3
votes
2 answers

Constant expression not representable in type 'UInteger'

If I run the following code in C# then it runs fine UInt32 a a = 0x9E3779B9 But when I run the same code in VB.Net then it gives me error "Constant expression not representable in type 'UInteger'" Dim a As UInt32 a = &H9E3779B9
Ali
  • 1,801
  • 6
  • 43
  • 58
2
votes
1 answer

Achieving a good compile-time error msg. based on value of parameter to factory function

I'm implementing a constant/immutable class in C++ 20 and I'd like to make as much as possible constant and do some value checks in my factory functions. Below is a simplified example (paste to godbolt.org) of what I'm trying to achieve. Please see…
jgreen81
  • 705
  • 1
  • 6
  • 14
2
votes
1 answer

Is `a` in `static const int a = std::random_device{}();` a glvalue core constant expression?

Cppreference states that the expression a defined in static const int a = std::random_device{}(); is a glvalue constant expression. That would mean that it must also be a core constant expression that is, among other conditions, its evaluation must…
domdrag
  • 541
  • 1
  • 4
  • 13
2
votes
0 answers

Why are member functions returning non-static data members not core constant expressions?

Consider the following example: (Demo) struct S { static bool const x = true; const bool y = true; constexpr bool f() const { return x; } constexpr bool g() const { return y; } }; int main() { const S s{}; …
mada
  • 1,646
  • 1
  • 15