So I was reading section 3.2 (One definition rule section) in the latest working draft of the C++ standard and came across this:
Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program; no diagnostic required.
This struck me as strange with regard to the variable part, because if a header file has a global constant variable and function like so:
const int k = 10;
int foo(const int &i) {
return 0;
}
What if you mistakenly call foo(k)
? It seems like this would result in UB because k
has internal linkage (i.e. every translation unit would have its own definition of k
, so there wouldnt be exactly one definition ... in that program). So basically if you have any const
variable with global scope you'd have to remember not to call any functions which take its reference or address.