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
43
votes
5 answers

Dividing by zero in a constant expression

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?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
41
votes
1 answer

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

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…
Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
33
votes
3 answers

Why can't a constant pointer be a constant expression?

The following program compiles: template class Test{}; extern const int var = 42; //extern needed to force external linkage int main() { Test<&var> test; } This one, however, doesn't, which is a surprise for me: template…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
31
votes
3 answers

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

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

Why can creating a static const std::string cause an exception?

I have string constants, for strings that I use in multiple places in my app: namespace Common{ static const std::string mystring = "IamAwesum"; } When posting a question about something else (What happens to a .h file that is not included in a…
Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190
24
votes
3 answers

Using function argument as part of a constant expression - gcc vs clang

Consider the following code snippet: template struct B { }; template constexpr bool pred(T t) { return true; } template auto f(T t) -> decltype(B{}) { } clang++ (trunk) compiles the code g++ (trunk)…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
24
votes
2 answers

Is the comma operator allowed in a constant-expression in C++11?

In the process of answering this question on SO for C++11, I realized that in C++03 (as well as in C) the use of the comma operator is explicitly forbidden in a constant-expression. Paragraph 5.19/1 of the C++03 Standard on constant expressions…
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
23
votes
3 answers

When does the 'const' qualifier guarantee the variable to be a constant expression?

As far as I know, the const qualifier in C++ basically declares internal linkage, and sometimes it allows the variable to be used as a constant expression so that it can be placed into array bounds, switch cases, etc. But apparently this is not…
morimn
  • 513
  • 3
  • 10
21
votes
1 answer

Why was the restriction on the comma operator being in a constant expression removed in C++11?

Recently when answering a question I realized that the comma operator is allowed in a constant expression in C++11 as long as the expression is surrounded by (), for example: int a[ (1, 2) ] ; Pre C++11 it is forbidden to use the comma operator in…
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
20
votes
3 answers

Pointer conversion issue with Ternary operator

I know the ternary operator has some surprising restrictions, but I was a bit baffled that this fails to compile for me: void foo(bool b) { int* ptr = ((b) ? NULL : NULL); } Obviously that's the minimum needed to show the problem. The error…
Roddy
  • 66,617
  • 42
  • 165
  • 277
20
votes
1 answer

Why are lambda expressions not allowed in an unevaluated operands but allowed in the unevaluated portions of constant expressions?

If we look at the draft C++ standard section 5.1.2 Lambda expressions paragraph 2 says (emphasis mine going forward): The evaluation of a lambda-expression results in a prvalue temporary (12.2). This temporary is called the closure object. A…
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
19
votes
7 answers

error: switch quantity not an integer

I have researched my issue all over StackOverflow and multi-google links, and I am still confused. I figured the best thing for me is ask... Im creating a simple command line calculator. Here is my code so far: const std::string…
Ken
  • 641
  • 3
  • 11
  • 25
17
votes
1 answer

Why "initializer element is not a constant" is... not working anymore?

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…
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
17
votes
2 answers

Constant expression initializer for static class member of type double

In C++11 and C++14, why do I need constexpr in the following snippet: class Foo { static constexpr double X = 0.75; }; whereas this one produces a compiler error: class Foo { static const double X = 0.75; }; and (more surprisingly) this…
Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
16
votes
1 answer

Can the C# compiler or JIT optimize away a method call in a lambda expression?

I'm starting this question after a discussion which started (in comments) on another StackOverflow question, and I'm intrigued to know the answer. Considering the following expression: var objects = RequestObjects.Where(r => r.RequestDate >…
ChristopheD
  • 112,638
  • 29
  • 165
  • 179
1
2 3
14 15