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
0
votes
1 answer

Why can't this const argument match a non-type template parameter?

When compiling the code below with gcc, I get an error: 'i' cannot appear in a constant-expression. Why is this? #include using namespace std; template class C { public: void test(); }; template void C

::test() { …

hu wang
  • 411
  • 3
  • 12
-1
votes
1 answer

constant expression required in switch with string

I have declared this enum class: public enum class RoleName { Worker, Boss } which I want to use it in a switch / case switch (requestRole.getName()) { case RoleName.Worker.name(): but I have this compilation error constant expression…
Sandro Rey
  • 2,429
  • 13
  • 36
  • 80
-2
votes
2 answers

objects that're usable constant expressions

I have the following code that demonstrate my problem: int main(void) { const int ci = 42; constexpr int j = ci; } The above program compiles fines. But I'm expecting it to be ill-formed. First, the initializer 42 is an integral constant…
mada
  • 1,646
  • 1
  • 15
-3
votes
2 answers

(C++) What exactly does this expression do? numeric_limits::is_signed

I came accross the following in a C++ textbook: bool result3 = numeric_limits::is_signed; //result3 holds a value of true bool result4 = numeric_limits::is_integer; //result4 holds a value of true bool result5 =…
-4
votes
3 answers

C11 and constant expression evaluation in switch-case labels

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…
Kochise
  • 504
  • 5
  • 10
1 2 3
14
15