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
0
votes
2 answers

Why putting inline function def in other header file which include the header file where the inline function declaration in is a link error?

I have two headers. // header1.h class A { public: void f(); }; // header2.h #include "header1.h" inline void A::f() { std::cout << "Yahoo."; } // test1.cpp #include "header1.h" int main() { A a; a.f(); return 0; } // test2.cpp #include…
Huang-zh
  • 147
  • 8
-1
votes
4 answers

How to specialize a template without specifying a class name?

I want to make a function called debug that outputs some info about objects. My system contains objects of many different types; some of them contain other objects. using namespace std; // for brevity struct dog {string name;}; struct human {string…
anatolyg
  • 26,506
  • 9
  • 60
  • 134
-1
votes
1 answer

Why function template can exist in multiple source files

I know the one definition rule and why the function definition should not be put in the header file if you want to use it in multiple source files. However, I don't quite get the reason why we can put the function template in the header file. During…
MinLinC
  • 47
  • 2
-1
votes
1 answer

odr-use of `static const int` data member succeeds WITHOUT definition in call taking `const int &`

This is a frustratingly difficult question to ask. I'm asking you to guess what code or conditions in my build system might allow the following to build and run correctly, when the obvious expectation is that it should result in a linker undefined…
MikeW
  • 89
  • 1
  • 4
-1
votes
1 answer

Should I protect against ODR violations when declaring function templates in source files?

An "ordinary" function, when defined and used exclusively in a single translation unit is declared and defined like this: // implementation.cpp static void fun(int arg) { /* implementation */ } alternatively you could drop the static keyword and…
Lorah Attkins
  • 5,331
  • 3
  • 29
  • 63
-1
votes
2 answers

Equivalence of static function for methods

Sometimes, I want to have a function in a header file (included in multiple different translation units) without telling the compiler to inline it (for example in a header-only library). This is easy when doing it the C-style way, just declare the…
Remember Monica
  • 3,897
  • 1
  • 24
  • 31
-1
votes
1 answer

Visual Studio separate .h and .cpp files for a set of functions

I'm trying to get used to Visual Studio's c++ environment and I'm having an issue with a set of functions which I wish to define into their own .h and .cpp files. Within my project, there is a file containing some common variables and definitions…
Zulukas
  • 1,180
  • 1
  • 15
  • 35
-1
votes
1 answer

Inline function: Access violation reading location 0xcdcdcdd5?

I had run into a strange problem, where I used to get an exception at the run-time with the message: Unhandled exception at 0x0137d451 in I2cTlmTest.exe: 0xC0000005: Access violation reading location 0xcdcdcdd5. I get this message during creation…
Uchia Itachi
  • 5,287
  • 2
  • 23
  • 26
-2
votes
1 answer

Can a function declared in header have multiple definitions in different source files all in one project?

I had an interview today and the panelist asked me this question. I said the compiler will throw an error stating multiple declarations for the function. He said its possible using OOPS. Can anyone help me out here.
-2
votes
2 answers

Why does a struct declaration violate the ODR in C++?

I am trying to get the compiler to react to some code that I believe does not violate the one-definition-rule in C++. Inside a header file, I have two declarations: one for a struct and one function, like this: struct TestStruct { int a; …
softwarelover
  • 1,009
  • 1
  • 10
  • 22
-5
votes
1 answer

C++ Function already defined in .obj

Fairly new to C++ and not exactly sure what is happening. I've done this for other classes but for this one for some reason i keep getting this error (I also get this error for the start focus function): LNK2005 "public: void __cdecl…
1 2 3
20
21