Questions tagged [one-definition-rule]

Anything related to C++ One Definition Rule (ODR), i.e. a rule of the C++ standard banning multiple definitions of most language entities. The ODR roughly mandates that most language entities (objects, functions, templates, etc.) must have a unique (non-duplicated) definition in the same translation unit or across the entire program, while multiple declarations are still possible.

The One Definition Rule (ODR) is the concept that there is at most one defined instance of a function or object allowed in a program.

In C, the One Definition Rule is described in C11 Section 6.9 in paragraph 3:

There shall be no more than one external definition for each identifier declared with internal linkage in a translation unit. Moreover, if an identifier declared with internal linkage is used in an expression (other than as a part of the operand of a sizeof or _Alignof operator whose result is an integer constant), there shall be exactly one external definition for the identifier in the translation unit.

In C++, the One Definition Rule is explained in C++11 Section 3.2 [basic.def.odr], but succinctly summarized in the first paragraph:

No translation unit shall contain more than one definition of any variable, function, class type, enumeration type, or template.

Inlined functions have their own exception clauses. Extensions to the C language relax the one definition rule strictness by permitting multiple definitions if their declarations are all compatible.

311 questions
19
votes
2 answers

Impossibly Fast C++ Delegates and different translation units

According to Sergey Ryazanov, his Impossibly Fast C++ Delegates are not comparable: My delegates cannot be compared. Comparison operators are not defined because a delegate doesn't contain a pointer to method. Pointer to a stub function can be…
Ash
  • 547
  • 5
  • 13
18
votes
3 answers

Is it safe to use #ifdef guards on C++ class member functions?

Suppose you have the following definition of a C++ class: class A { // Methods #ifdef X // Hidden methods in some translation units #endif }; Is this a violation of One Definition Rule for the class? What are the associated hazards? I suspect if…
18
votes
3 answers

Passing literal as a const ref parameter

Imagine the following simplified code: #include void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Optimizations aside, what happens when 42 is passed to foo? Does the compiler stick 42 somewhere (on…
18
votes
2 answers

GoogleTest PrintTo not getting called for a class

I'm having a rather strange problem telling googletest to print a certain class the way I want using PrintTo. The class is a very simple 2D point, it is in a namespace and the PrintTo function is in the same namespace. In fact, I have a derived…
MikMik
  • 3,426
  • 2
  • 23
  • 41
18
votes
3 answers

Why is there no multiple definition error when you define a class in a header file?

I'm not sure if I asked the question correctly, but let me explain. First, I read this article that explains the difference between declarations and definitions: http://www.cprogramming.com/declare_vs_define.html Second, I know from previous…
codecitrus
  • 659
  • 6
  • 17
17
votes
2 answers

anonymous namespaces and the one definition rule

Am I violating the One Definition Rule with the following program? // foo.hpp #ifndef FOO_HPP_ #define FOO_HPP_ namespace { inline int foo() { return 1; } } inline int bar() { return foo(); } #endif //EOF and // m1.cpp #include…
Lambdageek
  • 12,465
  • 1
  • 25
  • 33
17
votes
1 answer

Odd behavior passing static constexpr members without definitions by value

I was surprised to find that GCC and Clang disagree on whether to give me a linker error when passing a static constexpr member by value when there is no out-of-class definition: #include #include #include…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
16
votes
1 answer

static keyword in h file and internal linkage

Yet another static question. I have read the following: What are static variables? file scope and static floats http://msdn.microsoft.com/en-us/library/s1sb61xd.aspx And I still fail to understand the following behavior: I have one h file: //…
Itamar Katz
  • 9,544
  • 5
  • 42
  • 74
16
votes
4 answers

Why doesn't including headers with templates cause linker errors?

If I include or in multiple translation units (different .cpp files), why doesn't it break the ODR? As far as I know, each .cpp is compiled differently, so std::vector's member functions will be generated for each object file…
barney
  • 2,172
  • 1
  • 16
  • 25
16
votes
4 answers

If I don't odr-use a variable, can I have multiple definitions of it across translation units?

The standard seems to imply that there is no restriction on the number of definitions of a variable if it is not odr-used (§3.2/3): Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that…
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
15
votes
3 answers

Are different translation units allowed to define structures with the same name?

Suppose I have a.c and b.c, which both define types called struct foo, with different definitions: #include struct foo { int a; }; int a_func(void) { struct foo f; f.a = 4; printf("%d\n", f.a); return f.a *…
user253751
  • 57,427
  • 7
  • 48
  • 90
15
votes
1 answer

constexpr global of class type

My understanding is that constexpr globals of class type are all but unusable because Such an object must be defined in every TU, because constexpr does not permit forward declaration of an object. Default linkage as static would cause naming the…
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
15
votes
6 answers

C the same global variable defined in different files

I am reading this code from here(in Chinese). There is one piece of code about testing global variable in C. The variable a has been defined in the file t.h which has been included twice. In file foo.c defined a struct b with some value and a main…
liuan
  • 299
  • 1
  • 3
  • 9
14
votes
2 answers

Why does the same class being defined in multiple .cpp files not cause a linker multiple definition error?

I'm getting a strange behavior which I don't understand. So I have two different classes with the same name defined in two different cpp files. I understand that this will not cause any error during the compilation of the translation units as they…
Henry Wise
  • 367
  • 1
  • 9
14
votes
0 answers

Should I care about a one-definition-rule violation in a Rust executable reported by ASAN?

I have a Rust executable which was compiled purely within the Rust ecosystem; no external C code or linked libraries, sans whatever the compiler drags in. After compiling it with ASAN on Linux, ASAN reports a one-definition-rule (ODR) violation.…
user
  • 4,920
  • 3
  • 25
  • 38
1
2
3
20 21