Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 342
fredoverflow Avatar asked Oct 20 '11 09:10

fredoverflow


People also ask

Can you put function definitions in a header file?

The answer to the above is yes. header files are simply files in which you can declare your own functions that you can use in your main program or these can be used while writing large C programs. NOTE:Header files generally contain definitions of data types, function prototypes and C preprocessor commands.

Can you put functions in header files C?

In C, you cannot have the function definition/implementation inside the header file.


2 Answers

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.

like image 85
David Rodríguez - dribeas Avatar answered Nov 15 '22 18:11

David Rodríguez - dribeas


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

like image 25
BЈовић Avatar answered Nov 15 '22 18:11

BЈовић