ODR 使用的规则不适用于 visual studio
odr-used rule does not apply in visual studio
Informally, an object is odr-used if its value is read (unless it is a compile time constant) or written, its address is taken, or a reference is bound to it; ..... If an object, a reference or a function is odr-used, its definition must exist somewhere in the program; a violation of that is usually a link-time error.
但在 visual studio 2015 年:
#include <iostream>
using namespace std;
class A{
public:
static const int k=666;
};
int main() {
const int&s=A::k;//no warning?undefined behavior?
cout << &A::k << endl;//no warning,no error?
return 0;
}
根据 cppref:
a definition outside of class is required if it is odr-used
Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program outside of a discarded statement; no diagnostic required.
强调我的。
Informally, an object is odr-used if its value is read (unless it is a compile time constant) or written, its address is taken, or a reference is bound to it; ..... If an object, a reference or a function is odr-used, its definition must exist somewhere in the program; a violation of that is usually a link-time error.
但在 visual studio 2015 年:
#include <iostream>
using namespace std;
class A{
public:
static const int k=666;
};
int main() {
const int&s=A::k;//no warning?undefined behavior?
cout << &A::k << endl;//no warning,no error?
return 0;
}
根据 cppref:
a definition outside of class is required if it is odr-used
Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program outside of a discarded statement; no diagnostic required.
强调我的。