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
4
votes
2 answers

Determine whether an expression's value is known at compile time

Suppose I want to create a NonZero type so that my integer division function is total: def div(numerator: Int, denominator: NonZero): Int = numerator / denominator.value I can implement this by creating a NonZero class with a private…
rampion
  • 87,131
  • 49
  • 199
  • 315
4
votes
2 answers

Is reading one-past-the-end pointer allowed in a constant expression when short-circuit disable its evaluation

Consider example: template struct foo { }; int main() { foo<""[0]?""[1]:'\0'>{}; } The code compiles in both [gcc] and [clang], but should it really? I know the expression ""[1] doesn't need to be evaluated as it was short-circuited.…
W.F.
  • 13,888
  • 2
  • 34
  • 81
4
votes
3 answers

Will the Java compiler precalculate sums of literals?

int i = 10 + 20; Is it true that the compiler will process this code, adding 10 + 20, and the byte code is the same as for this line of code? int i = 30; Where can I read about it?
user471011
  • 7,104
  • 17
  • 69
  • 97
4
votes
4 answers

Need clarification about constant expressions

K&R c 2nd edition(section 2.3) mentions A constant expression is an expression that involves only constants. Such expressions may be evaluated at during compilation rather than run-time, and accordingly may be used in any place that a constant can…
Raman
  • 2,735
  • 1
  • 26
  • 46
4
votes
1 answer

Cannot create list literal in F#

I have the following types type StatusCode = | OK = 200 | NoContent = 204 | MovedTemp = 301 | MovedPerm = 302 | SeeOther = 303 | NotModified = 304 | NotFound = 404 | ServerError =…
Overly Excessive
  • 2,095
  • 16
  • 31
4
votes
1 answer

What do the words "undefined" mean in bullet point §5.19/2.3 in N4140?

From N4140 §5.19/2.3 (emphasis mine) — an invocation of an undefined constexpr function or an undefined constexpr constructor; From §7.1.5/2 constexpr functions and constructors are implicitly inlined, that is, if a constexpr function is not…
Belloc
  • 6,318
  • 3
  • 22
  • 52
4
votes
1 answer

Constant variable vs constant reference

In some C++ sources I saw that an expression result can be saved as a constant reverence. Like this: const int &x = y + 1; What does it mean? Is there any documentation on this? I can't find it.. For me it seems to be equivalent to: const int x…
klm123
  • 12,105
  • 14
  • 57
  • 95
3
votes
2 answers

Constant expression confusion

I am reading an example of cppreference at https://en.cppreference.com/w/cpp/language/constant_expression Specifically, this one: constexpr int incr(int& n) { return ++n; } constexpr int g(int k) { constexpr int x = incr(k); // error:…
3
votes
1 answer

What exactly is a C++ constant subexpression?

The C++ standard, Section 3.14 [1], says: 3.14[defns.const.subexpr] constant subexpression expression whose evaluation as subexpression of a conditional-expression CE would not prevent CE from being a core constant expression I'm not sure how to…
cmutex
  • 1,478
  • 11
  • 24
3
votes
1 answer

C++ logging source line of an object instantiation as a constant expression without a macro

I'm playing around with std::source_location as a means to define a static identifier for a class which behaves similar to a compile-time counter. The basic idea is rather simple: use a class template taking an integral_constant as template…
davidhigh
  • 14,652
  • 2
  • 44
  • 75
3
votes
1 answer

Why initializing a 'const reference to base' object from 'constexpr derived' object doesn't yields constant expression?

I have this simple example: struct base { constexpr base () {}; }; struct derived: public base { constexpr derived () {}; }; int main() { constexpr derived d{}; constexpr const base &b = d; // why error? } G++ gives me the…
mada
  • 1,646
  • 1
  • 15
3
votes
1 answer

How can I tell if an elisp expression is pure and constant?

At some point, Emacs added the pure symbol property, indicating which functions are known to be pure (see here). Is there a way to use this property to determine if an entire expression is constant and side-effect-free? For example, it's pretty easy…
Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159
3
votes
0 answers

How are trait objects handled inside const functions?

I was trying to create an uninitialized boxed trait object wrapped inside a MaybeUninit. trait Foo {} const fn test() { // Error! let _bar: MaybeUninit> = MaybeUninit::uninit(); } playground link The compiler gives me the…
Gymore
  • 585
  • 2
  • 10
3
votes
1 answer

Why are `const` pointers to functions not usable in a constant expression?

Consider the following template: using IntFnPtr = int(*)(int); template void f() { } And these tests: int g(int) { } int main() { f<&g>(); // OK const IntFnPtr cp = &g; f(); // Error -- why? constexpr IntFnPtr…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
3
votes
2 answers

what situation will the evaluation for expressions occur in

constexpr int func(int rf){ constexpr int v = rf; // #1 return 0; } int main(){ } Consider the above code, the compiler complains such a code is ill-formed. The outcome is here: error: 'rf' is not a constant expression That is said, the…
xmh0511
  • 7,010
  • 1
  • 9
  • 36