1 year ago

#227276

test-img

Adam

Enable warnings for one definition rule violations

There's a particular ODR violation that results from innocent enough code.

// TU1.cpp
struct Foo
{
    int Bar() { return 1; }
};

// TU2.cpp
struct Foo
{
    int Bar() { return 2; }
};

These two classes have no relation to each other. They are purely implementation details in unrelated cpp files. They just happen to have the same name and a function with the same signature. Member functions have external linkage by default and the functions are inline by default because they are defined inside the class. So the linker sees 2 definitions for Foo::Bar, assumes they are the same, and silently throws one of them away.

I'd like to be able to catch this specific bug, but so far I can't find any options for any of the big three compilers/linkers that will actually catch this. Does such an option exist? Is there another way to catch these kinds of bugs that isn't "wrap every class in every cpp in an anonymous namespace"?

Here's a working example of this specific issue. https://godbolt.org/z/as9frG5o3

c++

linker

one-definition-rule

0 Answers

Your Answer

Accepted video resources