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

"Constant expressions" prior to C++11

The constexpr keyword was introduced in C++11, as (I think) was the corresponding idea of "constant expressions." However, this concept was implicitly present in C++98/c++03, since array declarations require a constant expression: // valid: int…
Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
15
votes
2 answers

Confusion about constant expressions

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…
Morwenn
  • 21,684
  • 12
  • 93
  • 152
14
votes
3 answers

Why can't a placement-new expression be a constant expression?

According to [expr.const]/5.18: An expression E is a core constant expression unless the evaluation of E, following the rules of the abstract machine ([intro.execution]), would evaluate one of the following: a new-expression ([expr.new]), unless…
14
votes
2 answers

__PRETTY_FUNCTION__ in constant expression

Please refer to this snippet: #include #include constexpr std::size_t strlen(char const* s) { std::size_t n = 0; while (*s++ != '\0') ++n; return n; } template struct X {}; int main()…
nicolai
  • 1,140
  • 9
  • 17
14
votes
2 answers

Why is this addition being silently ignored?

I have the following C code: #include #include int i; uint64_t a[] = { (uint64_t)&i, (uint64_t)&i + 0x8000000000000000 }; int main() { printf("%p %llx %llx\n", &i, a[0], a[1]); } If I compile this (as C or as C++) with…
user200783
  • 13,722
  • 12
  • 69
  • 135
14
votes
2 answers

Endianness in constexpr

I want to create a constexpr function that returns the endianness of the system, like so: constexpr bool IsBigEndian() { constexpr int32_t one = 1; return (reinterpret_cast(one) == 0); } Now, since the function will get…
Rick de Water
  • 2,388
  • 3
  • 19
  • 37
13
votes
1 answer

gcc and clang disagree on whether expression is constant evaluated

For the following program: struct S { int i; }; constexpr S f() { return std::is_constant_evaluated() ? S{1} : S{0}; } int main() { S s = f(); return s.i; } gcc returns 0, and clang returns 1. demo I don't think the evaluation of f is…
cigien
  • 57,834
  • 11
  • 73
  • 112
12
votes
2 answers

Why are function addresses not constant expressions

Is there a way to use function addresses in constant expressions? void foo() {} int main() { static_assert(&foo, "test error"); } This won't compile. error C2057: expected constant expression The intention behind this is that I want to…
cooky451
  • 3,460
  • 1
  • 21
  • 39
12
votes
2 answers

Why doesn't a Java constant divided by zero produce compile time error?

Possible Duplicate: Is 1/0 a legal Java expression? Why does this code compile? class Compiles { public final static int A = 7/0; public final static int B = 10*3; public static void main(String[] args) {} } If I take a look in the…
12
votes
1 answer

Is assert usable in constant expressions?

The assert-macro from 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?
Columbo
  • 60,038
  • 8
  • 155
  • 203
12
votes
2 answers

Java constant expressions and code elimination

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…
12
votes
2 answers

Is gcc considering builtins of non-constant expression functions to be constant expressions

Please see the update for a better sample of the problem. The original code has a mix of issues which muddies the picture: This question Why can I call a non-constexpr function inside a constexpr function? presented the following code #include…
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
11
votes
2 answers

Can a null pointer constant be any integer constant expression evaluated to 0?

The standard says: "An integer constant expression with the value 0, or such an expression cast to type void*, is called a null pointer constant.67) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null…
11
votes
1 answer

Is indexing a string literal an initializer constant expression?

The following code attempts to use array indexing on a string literal in two different constant contexts: static char x = "abcx"[3]; _Static_assert ("abcx"[3] == 'x', "..."); Judging by Compiler Explorer, there's clear consensus among tool vendors…
Alex Celeste
  • 12,824
  • 10
  • 46
  • 89
11
votes
3 answers

Why is a constexpr function on a reference not constexpr?

Consider the following function: template auto concatenate(std::array &data1, std::array &data2) { std::array result; auto iter =…
Martijn Otto
  • 878
  • 4
  • 21
1
2
3
14 15