tags:

views:

101

answers:

3

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.

+9  A: 

The problem is that, even though s1 and s2 have only internal linkage, both of the corresponding definitions of S have external linkage.

What you want to do is to use an anonymous namespace:

//File1.cpp
#include "StdAfx.h"
namespace {
    struct S { int Value() { return 1; } } s1;
}
int GetValue1() { return s1.Value(); }

//File2.cpp
#include "StdAfx.h"
namespace {
    struct S { int Value() { return 2; } } s2;
}
int GetValue2() { return s2.Value(); }

Edit:

Everything inside the anonymous namespace, including the class definitions, has internal linkage.

Definitions in the anonymous namespace still have external linkage, but the compiler ensures that they receive unique names that won't clash with any definitions from other translation units.

Martin B
Simply not true. Being inside an unnamed namespace has no effect on any linkage.
Charles Bailey
I know how to fix the problem. I was looking to why it was a 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.
jyoung
The unnamed namespace doesn't affect the linkage, it just puts the contents in a namespace that's distinct from any other. They still have external linkage.
Mike Seymour
@Charles and @Mike: Thanks -- didn't know that. I'll update the answer accordingly.
Martin B
@jyoung: This is the bit of the ODR that you're violating: "Some things, like types, templates, and extern inline functions, can be defined in more than one translation unit. For a given entity, each definition must be the same."
Martin B
+9  A: 

This is unsafe because you have two structs named S. The static keyword only applies to the variable declaration; it's equivalent to you having written:

struct S {
    int Value() {return 1;}
};

static S s1;

The compiler doesn't notice this at compile time because it deals with each translation unit separately. The Value functions in the structs are mangled to exactly the same name, and become weak global symbols in the object files, so the linker doesn't throw an error about a symbol name collision; it just picks one to use in the fully-linked binary. This will probably be the first symbol definition, which means you can actually get different behavior depending on the order you linked the objects:

> g++ -o test test.o test1.o test2.o && ./test
s1 is 1
s2 is 1

> g++ -o test test.o test2.o test1.o && ./test
s1 is 2
s2 is 2

You can get around this by either wrapping the structs in anonymous namespaces (which will make the Value function symbols locals instead of weak globals):

namespace {
    struct S {
        int Value() {return 1;}
    } s1;
}

Or simply removing the name of the struct, since you don't actually need it:

struct {
    int Value() {return 1;}
} s1;
Michael Mrozek
"simply removing the name of the struct..." I didn't know one could leave a struct unnammed when declaring it. I think it is also possible with class ?
Stephane Rolland
@Stephane It is possible with `class`, but that usage is extremely rare. Naming a struct is essentially just typedefing it so you can use the name later to refer to the struct as a whole, but you can just keep redefining the anonymous struct each time. For example: `struct {int x;} foo() {struct {int x;} rtn; rtn.x = 4; return rtn;}` will declare a function `foo` that returns a struct, and you could call it with `struct {int x;} var = foo();`
Michael Mrozek
@Michael, maybe usage is extremely rare, but now I know it's possible. Thanx for the example.
Stephane Rolland
+6  A: 

You have defined struct S in the global namespace in two different ways, which breaks the One Definition Rule. In particular, there are two different definitions of ::S::Value(), and it's undefined which will actually end up being called.

You should use nameless namespaces to make sure a distinctly named version of struct S is defined in each header:

namespace { struct S {int Value() {return 1;}} s1; }
int GetValue1() {return s1.Value();}

There's a lot more the the One Definition Rule than the first paragraph which you quote. The last paragraph basically says that some things, including class definitions, can appear more than once in a program, as long as they are all identical. Your code breaks this last condition. Or, in the (abridged) words of the Standard:

There can be more than one definition of a class type ... in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. Given such an entity named D defined in more than one translation unit, then each definition of D shall consist of the same sequence of tokens.

Mike Seymour
Technically, it's an allowed exception to have two definitions of ::S::Value() as both are (implicitly) declared inline (`inline` functions may be defined in mulitple TUs and must be defined in every TU where they are used). What isn't allowed is that the definitions aren't the same.
Charles Bailey
@Charles: yes, I could have been clearer about that. Updating...
Mike Seymour