I'm looking for some secure coding guideline and came across the SEI CERT C++ Coding Standard.
Most things are clear so far, but I don't understand the last Noncompliant Code Example from DCL60-CPP: Obey the one-definition rule.
In this noncompliant code example, the constant object n has internal linkage but is odr-used within f(), which has external linkage. Because f() is declared as an inline function, the definition of f() must be identical in all translation units. However, each translation unit has a unique instance of n, resulting in a violation of the ODR.
const int n = 42;
int g(const int &lhs, const int &rhs);
inline int f(int k) {
return g(k, n);
}
I tried to put the shown code in a header file and included it in two separate cpp files. I then compiled it with clang++ and g++. Both without warnings. It executed normally.
Edit: So what I don't understand is how or under what circumstances the shown Example violates the ODR.
constexprmight solve issues, or some tricks such asreturn g(k, +n);. - Jarod42