I was told that these types, that are visible in there own unique translation unit, were in violation of the One Definition Rule. Can someone explain this?
//File1.cpp
#include "StdAfx.h"
static struct S { int Value() { return 1; } } s1;
int GetValue1() { return s1.Value(); }
//File2.cpp
#include "StdAfx.h"
static struct S { int Value() { return 2; } } s2;
int GetValue2() { return s2.Value(); }
// main.cpp
#include "stdafx.h"
extern int GetValue1();
extern int GetValue2();
int _tmain(int argc, _TCHAR* argv[])
{
if( GetValue1() != 1 ) throw "ODR violation";
if( GetValue2() != 2 ) throw "ODR violation";
return 0;
}
I know how to fix the problem. As per the title, I was looking to why it was a ODR violation. How does it violate: "In any translation unit, a template, type, function, or object can have no more than one definition."? Or maybe it violates a different part of the rule.