The code below compiles in GCC, clang and VS2017 and the expression a->i
in the return
statement is replaced by its constant value 1. Is it correct to say that this is valid because a
is not odr-used in the expression a->i
?.
struct A
{
static const int i = 1;
};
int f()
{
A *a = nullptr;
return a->i;
}
PS: I believe a
is not odr-used in the expression a->i
because it satisfies the "unless" condition in [basic.def.odr]/4, as follows:
A variable
x
whose name appears as a potentially-evaluated expressionex
is odr-used byex
unless applying the lvalue-to-rvalue conversion (7.1) tox
yields a constant expression (8.6) that does not invoke any non-trivial functions and, ifx
is an object,ex
is an element of the set of potential results of an expressione
, where either the lvalue-to-rvalue conversion (7.1) is applied toe
, ore
is a discarded-value expression (8.2).
In particular, the expression ex == a
is an element of the set of potential results of the expression e == a->i
, according to [basic.def.odr]/2 (2.3), containing the expression ex
, where the lvalue-to-rvalue conversion is applied to e
.