简体   繁体   中英

putting function definitions in header files

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 function as static
  3. put the function in an anonymous namespace

(Until recently, I wasn't even aware of #1.) So what are the differences to these solutions, and when I should I prefer which? I'm in header-only world, so I really need the definitions in the header files.

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 .

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM