1

I have a declaration of an inline function that happens to be recursive. Since it's recursive there is no point in declaring it inline so why does my linkage fail when i remove it?

3 files:

\\File1.h

#ifndef FILE1_H
#define FILE1_H

inline int Factorial(int a)
{
    if (a < 2)
        return 1;
    return a*Factorial(a-1);
}

int PermutationsNum(int b);


#endif


\\File1.cpp
#include "File1.h"

int PermutationsNum(int b)
{
    return Factorial(b);
}


\\File2.cpp

#include <iostream>
#include "File1.h"

int main()
{
    std::cout << "permutations of 7 elements: " << PermutationsNum(7) << std::endl;
    return 0;
}
Tom
  • 9,275
  • 25
  • 89
  • 147

1 Answers1

5

inline tells the compiler not to export the symbol. If you don't use it, the symbol will be exported by all compilation units that include that file, thus resulting in a multiple definition.

3.2 One definition rule [basic.def.odr]

4) Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is implicitly defined (see 12.1, 12.4 and 12.8). An inline function shall be defined in every translation unit in which it is odr-used.

This is the only pertinent use of the keyword inline in fact - actually inlining functions is up to the compiler.IMO, the keyword is even less than a hint in that sense.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625