5

In the following...

struct C {};
constexpr C c;

void g(C);

template<typename T>
void f(T&& t) {
  g(std::forward<T>(t));
}

int main() {
  f(c);
}

Is c odr-used? Why / why not?

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319

1 Answers1

4

Going through the same motions as in Richard's answer, we find that the second condition for not being odr-used is violated, and thus c is odr-used. In detail, the condition reads:

[A variable x is odr-used by an expression ex unless x is an object and] ex is an element of the set of potential results of an expression e, where either the lvalue-to-rvalue conversion is applied to e, or e is a discarded-value expression.

In our case x from the Standard is your c, and ex is the id-expression c. The only expressions of which ex is a potential result is the id-expression ex itself. It is neither a discarded-value expression, nor is the lvalue-to-rvalue conversion applied to it (since it binds to a reference).

Community
  • 1
  • 1
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • So what is the actual answer to the question that you link to? There are two answers (+12 and +1) for "yes it is odr-used" and one (+4) for "no it's not." – Barry Jan 08 '15 at 18:48
  • @Barry: That's why I linked to the answer, not the question :-) – Kerrek SB Jan 08 '15 at 18:49