putting function definitions in header files

后端 未结 4 493
情话喂你
情话喂你 2021-02-05 14:22

If you want to put function definitions in header files, it appears there are three different solutions:

  1. mark the function as inline
  2. mark the
相关标签:
4条回答
  • 2021-02-05 14:27

    As far as I know, only inline and template functions can be defined in header files.

    static functions are deprecated, and functions defined in an unnamed namespace should be used instead (see 7.3.1.1 p2). When you define a function in an unnamed namespace in a header, then every source code including that header (directly or indirectly) will have an unique definition (see 7.3.1.1 p1). Therefore, functions should not be defined in the unnamed namespace in header files (only in source files).

    The standard referenced are from the c++03 standard.

    EDIT:

    Next example demonstrates why functions and variables shouldn't be defined into unnamed namespace in headers :

    ops.hpp contains:

    #ifndef OPS_HPP
    #define OPS_HPP
    namespace
    {
    int a;
    }
    #endif
    

    dk1.hpp contains:

    #ifndef DK1_HPP
    #define DK1_HPP
    void setValue();
    void printValue();
    #endif
    

    dk1.cpp contains:

    #include "dk1.hpp"
    #include "ops.hpp"
    #include <iostream>
    
    void setValue()
    {
        a=5;
    }
    void printValue()
    {
        std::cout<<a<<std::endl;
    }
    

    dk.cpp contains :

    #include "dk1.hpp"
    #include "ops.hpp"
    #include <iostream>
    
    int main()
    {
        // set and print a
        setValue();
        printValue();
    
        // set and print it again
        a = 22;
        std::cout<<a<<std::endl;
    
        // print it again
        printValue();
    }
    

    Compile like this:

    g++ -ansi -pedantic -Wall -Wextra dk.cpp dk1.cpp
    

    and the output :

    5
    22
    5
    

    ops the variable a is different for the source file dk1.cpp and dk.cpp

    0 讨论(0)
  • 2021-02-05 14:35

    The static and unnamed namespace versions end up being the same: each Translation Unit will contain it's own version of the function, and that means that given a static function f, the pointer &f will be different in each translation unit, and the program will contain N different versions of f (more code in the binary).

    This is not the right approach to provide a function in a header, it will provide N different (exactly equal) functions. If the function contains static locals then there will be N different static local variables...

    EDIT: To make this more explicit: if what you want is to provide the definition of a function in a header without breaking the One Definition Rule, the right approach is to make the function inline.

    0 讨论(0)
  • 2021-02-05 14:41

    static functions (equivalent to anonymous namespace) receive different copies for each TU. If the function is re-entrant this is basically identical (some compilers might have assembly-level differences) but if it isn't then it will have different static data for each TU. Inline functions are folded- that is, they have only one copy of static data for every TU.

    0 讨论(0)
  • 2021-02-05 14:51

    You could consider wrapping the methods in a class instead of a namespace. Declare these methods as static and delete the constructor of the class to reinforce that this is not an object to be instantiated.

    struct FooNamespace
    {
        FooNamespace() = delete;
    
        static FooMethod1() {
            ...
        }
        static FooMethod2() {
            ...
        }
    
    };
    

    You get the same general behavior as it belonging to a namespace with only a single implementation.

    0 讨论(0)
提交回复
热议问题