4

(Following my recent question, and another question.)

[expr.const]/4 says that:

A converted constant expression of type T is an expression, implicitly converted to type T, where the converted expression is a constant expression and the implicit conversion sequence contains only

  • ... (list omitted)

and where the reference binding (if any) binds directly.

(Emphasis mine.)

There are two points that I do not quite understand here.

First, which expression does "the converted expression" (emphased) refer to?

Consider, for example

class A; // assume it can be implicitly converted to `int`
A foo(); // or maybe constexpr
template<int n> void bar();

Now, if I write

bar<foo()>();

then which expression should be constant expression? Should the whole foo() expression be constant, or just something like static_cast<int>(foo())?

From what I understand from my recent question, only the latter needs to be constant. Is this true?

Second, what stage of the whole process does "reference binding" refer to? Does it refer only to cases when the template parameter itself is a reference (template<int& x>...)? Or does it require that any reference binding that occurs somewhere during the type conversion or evaluation of converted expression, should be direct? Or does it refer to the case when the non-yet-converted expression is a reference itself (A& a=...; bar<a>();)?

Community
  • 1
  • 1
Petr
  • 9,812
  • 1
  • 28
  • 52

1 Answers1

5

First, which expression does "the converted expression" (emphased) refer to?

Implicit conversion introduces a correspondingly initialized temporary:

T e = /* original expression */;

e is the "converted expression". T = int in your case.

then which expression should be constant expression?

e.

Moreover, foo and the conversion operator function that has been implicitly invoked must be constexpr functions as per [expr.const]/(2.2).

Second, what stage of the whole process does "reference binding" refer to?

When T is a reference type, that reference - e in the above example - shall bind directly. No binding necessitated inside the expression or before it is of interest.

Columbo
  • 60,038
  • 8
  • 155
  • 203