0

Why the following example compile with no problems?

#include <iostream>
int main(){
  
  const int var1 = 2;
  constexpr int var2 = var1 * 5;

return 0;
}

According to theory: “Variables” that are not constant expressions (their value is not known at compile time)

I used gcc compiler, can be the case that each compiler behave different?

Then how const var1 is known at compile time in this example?

I found other topics about const vs constexpr but I still don't understand it.

Passer By
  • 19,325
  • 6
  • 49
  • 96
Mihai
  • 29
  • 8
  • `var1` is a constant expression because it is initialized by another constant expression. – Jason Oct 01 '22 at 11:54
  • 1
    `const int var1 = something()` COULD be a constant expression. `constexpr int var2 = something()` MUST be a constant expression. In your case `var1` is a constant expression since it is `const` and initialized with an integer literal. So the compiler doesn't mind. – DeiDei Oct 01 '22 at 11:56
  • Because `constexpr int var2` use in his initalization `var1` will force `var1` to be initialized at compile time ?! – Mihai Oct 01 '22 at 11:59
  • This is a special case. Any `const` integral types initialized with constant expressions are usable as constant expressions. It has nothing to do with `var2`. – Passer By Oct 01 '22 at 12:00
  • 1
    @Mihai Whether you defined `var2` or not is irrelevant. The point is that `var1` itself is a constant expression as it is a `cont` int variable whose initializer is a constant expression. This is explained in any beginner [c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Jason Oct 01 '22 at 12:00
  • 1
    See the explanation and same example in [this answer](https://stackoverflow.com/a/14117121/12002570). In particular because *"This is possible because var1, being constant and initialized at declaration time with a literal, satisfies the criteria for a constant expression, even if it isn't declared constexpr."* An even better dupe: [The value of a const variable is or is not usable in a constant expression, depending on the variable type](https://stackoverflow.com/questions/45593044/the-value-of-a-const-variable-is-or-is-not-usable-in-a-constant-expression-depe) – Jason Oct 01 '22 at 12:12
  • Thank you for help is great. Also I want to highlight the comment of @Jason Liam because from his link I found "C++ Primer" book where is explained very well this aspect. – Mihai Oct 01 '22 at 20:04

1 Answers1

1

Why the following example compile with no problems?

The full-expression of any constexpr variable has to be a constant expression, i.e evaluable at compile-time.

Your initializer var*5 is a constant expression because var is const-qualifed integral type that is itself initialized by integral constant expression; also 5 is also an integral constant expression, hence, the full-expression var*5 is also a constant expression. So nothing will cause the program to be ill-formed.

mada
  • 1,646
  • 1
  • 15