Search code examples
c++linkageone-definition-ruletranslation-unit

Could I provide same function definition in different TUs


I was reading about internal and external linkage, and I found that by default a function has an external linkage.

So I was thinking if is it possible to declare a function in a header filer and provide multiple definitions of it in different Translation units.

So far I did declare a funtion in a header file

void fct();

and provide 2 definitions in two files each of which is contained into an anonymous namespace:

namespace
{
    void fct()
    {    
    }    
}

But I didn't see how this could be a good examle of using multiple definitions of a function in different TU.

could someone show me a simple example of that (even using inline) thank you


Solution

  • Could I provide same function definition in different TUs

    If the function is not declared inline, then no; that would violate the one definition rule.

    A inline function with external linkage can be defined in multiple TUs - with additional requirement that the definition must be the same. In fact, the inline declaration would make it mandatory to provide the definition in all TUs that odr-use the function.

    and provide 2 definitions in two files each of which is contained into an anonymous namespace:

    That doesn't violate standard rules. Those two functions are not the same, nor are they the same as the global ::fct.

    But I didn't see how this could be a good examle of using multiple definitions of a function in different TU.

    could someone show me a simple example of that (even using inline) thank you

    Here you go:

    // header.hpp
    inline void foo() {}
    
    // a.cpp
    #include "header.hpp"
    
    // b.cpp
    #include "header.hpp"
    

    Here there are two TU's, each of which contain the definition for the function ::foo, included from the same header. This is allowed because the function is declared inline.


    Relevant standard quotes (current draft, irrelevant details redacted by me):

    One-definition rule [basic.def.odr]

    A ... function, ... shall not be defined where a prior definition is necessarily reachable ([module.reach]); no diagnostic is required if the prior declaration is in another translation unit.

    ...

    Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program outside of a discarded statement; no diagnostic required. ... A definition of an inline function or variable shall be reachable in every translation unit in which it is odr-used outside of a discarded statement.

    ...

    There can be more than one definition of a ... inline function with external linkage ([dcl.inline]) ... in a program provided that no prior definition is necessarily reachable ([module.reach]) at the point where a definition appears, and provided the definitions satisfy the following requirements. ... no diagnostic is required unless a prior definition is reachable at a point where a later definition appears. 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; and
    • ~ lenghy list of other limitations ~