Are variables appearing in function expression taking arguments by reference but returning by value odr-used?

Viewed 91

Given the code snippet:

struct S {
    static const int var = 0; 
}; 

int function(const int& rVar){
    return rVar; 
}

int main()
{
    return function(S::var); 
}

Compiled with gcc 5.4.0:

g++ -std=c++17 main.cpp -o test

results in the following linkage error:

/tmp/ccSeEuha.o: In function `main': main.cpp:(.text+0x15): undefined reference to `S::var' collect2: error: ld returned 1 exit status

§3.3 from the ISO Standard C++17 draft n4296 states:

A variable x whose name appears as a potentially-evaluated expression ex is odr-used by ex unless applying the lvalue-to-rvalue conversion (4.1) to x yields a constant expression (5.20) that does not invoke any non- trivial functions and, if x is an object, ex is an element of the set of potential results of an expression e, where either the lvalue-to-rvalue conversion (4.1) is applied to e [bold-type formatting added], or e is a discarded-value expression (Clause 5).

Q: Why is a definition of the variable var required here? Isn't var denoting an integer object that appears in the potentially-evaluted expression S::var of an outter function call expression, which indeed takes a parameter by reference, but to which finally a lvalue-to-rvalue conversion is applied, and, thus isn't odr-used as stated in the paragraph?

2 Answers
Related