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

Contradictory definitions about the Order of Constant Initialization and Zero Initialization in C++

I have been trying to understand how static variables are initialized. And noted a contradiction about the order of constant initialization and zero initialization at cppref and enseignement. At cppref it says: Constant initialization is performed…
user16783784
6
votes
1 answer

Is accessing a static constexpr member from a placement new a constant expression?

To clarify, is the following program well-formed? #include char foo[32]; struct bar { static constexpr int foobar = 42; }; int main() { auto p = new (foo) bar(); static_assert(p->foobar == 42); } gcc and msvc accept,…
6
votes
1 answer

Can "const fn" in rust concatenate byte slices?

When I compile the following code snippet: struct Packet([u8; 4]); impl Packet { const fn from(labels: [&[u8; 2]; 2]) -> Packet { let mut bytes = [0; 4]; bytes[..2].copy_from_slice(labels[0]); …
6
votes
1 answer

Java SE 11 String Final Variable with Ternary Operator Does Not Count as a Constant Variable in Switch Case Expression

I encountered a problem that the following code doesn't work. I ran the code in Java SE 11 (11.0.8), Eclipse 2020-06, Windows 10. Use String Final Variable with Ternary Operator: Doesn't work public class Tester { public static void…
6
votes
1 answer

Templated delegating copy constructor in constant expressions

This question is motivated by this one. Consider the following code: struct B {}; struct S { B b; // #1 S() = default; template // #2 constexpr S(const S&) {} template // #3 …
xskxzr
  • 12,442
  • 12
  • 37
  • 77
6
votes
1 answer

constexpr difference between gcc v10 and v9: bug or feature

If compiled with gcc v10 the code below exibits an error but for gcc v9 the code is ok. template struct A { constexpr auto size() const { return N; } }; template void foo1(const T& a) { constexpr auto s =…
wimalopaan
  • 4,838
  • 1
  • 21
  • 39
6
votes
1 answer

Implementing the Linux Kernel's __is_constexpr (ICE_P) macro in pure C++

After reading about the standard C11 version of Martin Uecker's ICE_P predicate, I tried to implement it in pure C++. The C11 version, making use of _Generic selection is as follows: #define ICE_P(x) _Generic((1? (void *) ((x)*0) : (int *) 0), int*:…
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
6
votes
2 answers

Is difference between two pointers legal c++17 constant expression?

According to cppreference section Core constant expressions point 19) a subtraction operator between two pointers is not legal constant expression until c++14. Can I assume that following code is legal c++17 code or is this interpretation an…
W.F.
  • 13,888
  • 2
  • 34
  • 81
6
votes
3 answers

Initializer with constant expression having possible overflow in C99

Is this valid C99 code? If so, does it define an implementation-defined behavior? int a; unsigned long b[] = {(unsigned long)&a+1}; From my understanding of the C99 standard, from §6.6 in the ISO C99 standard, this might be valid: An integer…
anol
  • 8,264
  • 3
  • 34
  • 78
6
votes
2 answers

Can arrays be indexed at compile time?

In this comment to another question, the user hvd stated the following: ... although string literals can be passed to constexpr functions, and array indexing is allowed on string literals in constant expressions, an indexing operation on a…
djsp
  • 2,174
  • 2
  • 19
  • 40
6
votes
1 answer

What is a core constant expression in the C++11 Standard?

There are 11 references to the expression core constant expression in the latest draft of the C++11 Standard (N3690), and none of them defines what this entity is. One can also find that the expression core constant expression is pretty well defined…
Wake up Brazil
  • 3,421
  • 12
  • 19
6
votes
4 answers

c#: what is a constant expression?

I'm working with attributes at the moment. I often run into the error 'An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type.' I don't really know what 'constant…
David
  • 15,750
  • 22
  • 90
  • 150
5
votes
1 answer

Conditional inclusion: integer constant expression is limited?

C11, 6.10.1 Conditional inclusion, Constraints, 1 (emphasis added): The expression that controls conditional inclusion shall be an integer constant expression C11, 6.6 Constant expressions, 6 (emphasis added): An integer constant expression117)…
5
votes
1 answer

The Requirement for "Literal Type" in "constexpr" Functions

Here is my code: class agg_t1{ int x; // private non-static data menber }; class agg_t2{ agg_t2(){} // user-provided constructor }; constexpr void ce1(agg_t1 arg){}; // OK constexpr void ce2(agg_t2 arg){}; // ERROR: …
5
votes
2 answers

what is the meaning of the phrase "preceding initialization" in section [expr.const]

constexpr int func(int const& rf){ return rf; } int main(){ constexpr int value = func(0); } Consider the above code, the variable value shall be initialized by a constant expression, which is func(0), which firstly shall be a core constant…
xmh0511
  • 7,010
  • 1
  • 9
  • 36