Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

odr-use of forwarded constexpr argument?

Tags:

c++

c++14

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?

like image 801
Andrew Tomazos Avatar asked Sep 29 '22 01:09

Andrew Tomazos


1 Answers

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).

like image 137
Kerrek SB Avatar answered Oct 04 '22 04:10

Kerrek SB