1

I have 2 header files that have identical content:

template<typename T> inline void func(){

}

I include these 2 headers in a main.cpp file then I compile:

g++ main.cpp -o run

But I get:

In file included from main.cpp:2:0:
test2.cpp:1:34: error: redefinition of ‘template<class T> void func()’
 template<typename T> inline void func(){
                                  ^
In file included from main.cpp:1:0:
test.cpp:1:34: error: ‘template<class T> void func()’ previously declared here
 template<typename T> inline void func(){

What am I getting this error if use inline function which can be redefined?

TheLogicGuy
  • 682
  • 8
  • 19
  • You're not allowed to have 2 definitions of the same function in the same translation unit. It doesn't matter if they're inline or not. – Barmar Jun 01 '17 at 13:02

3 Answers3

6

You missing a key piece. The standard says in [basic.def.odr]/6 that

There can be more than one definition of a class type (Clause 9), enumeration type (7.2), inline function with external linkage (7.1.2), class template (Clause 14), non-static function template (14.5.6), static data member of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization for which some template parameters are not specified (14.7, 14.5.5) in a program provided that each definition appears in a different translation unit[...]

emphasis mine

So, you're allowed to have have multiple definitions of the inline function, but those definitions need to be in separate translation units(basically source files). Since they are in the same translation unit, they are violating that and you get an error.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • When testing with two files that include the header, why using `template` without `inline` doesnt give error but removing `template` and `inline` keyword gives an error. Are all templates functions inline? – TheLogicGuy Jun 01 '17 at 13:07
  • @TheLogicGuy not all templates functions are inlines. – Yola Jun 01 '17 at 13:09
  • @Yola In my case is it online? – TheLogicGuy Jun 01 '17 at 13:09
  • @TheLogicGuy in your case the function defined inline, and likely to be inlined by compiler – Yola Jun 01 '17 at 13:10
  • @TheLogicGuy i've just checked and in release the template i thought wouldn't be inlined was also inlined. I think that's because definition is visible to the calling code. Intresting how it would behave with if template is externed. – Yola Jun 01 '17 at 13:28
  • @TheLogicGuy Yes, in this case, the template function is implicitly marked as inline. For the rules on this see: https://stackoverflow.com/questions/10535667/does-it-make-any-sense-to-use-inline-keyword-with-templates – NathanOliver Jun 01 '17 at 13:55
0

Because of One Definition Rule (ODR).

In any translation unit, a template, type, function, or object can have no more than one definition. Some of these can have any number of declarations. A definition provides an instance.

Yola
  • 18,496
  • 11
  • 65
  • 106
0

You're only allowed to define a function once in a translation unit. There's no exception for inline functions (they do get an exception for being defined in separate TUs that are linked together).

What you should do is move the function definition to its own header file, which then gets included by the original 2 headers. Then you can put a header guard in the new file, so the function only gets defined the first time it's included.

Barmar
  • 741,623
  • 53
  • 500
  • 612