Search code examples
c++header-filesauto

auto specifier in header file


I have a file Foo.h:

// Foo.h
#pragma once

int foo();

Also I have a Foo.cpp file which implements Foo.h:

//Foo.cpp
#include "Foo.h"

int foo() {
    return 5;
}

Is there a way to make foo function return type auto? It may be useful if I create a function which return value depends on 3rd party library functions, which return value types may change in later versions.


Solution

  • If you want to do that, you need to put the definition of the function (not just the declaration) into the header file. Otherwise, how can the compiler tell the type of foo if it is called from another source file?

    And if you want to define a function in a header file, you need to declare it inline, otherwise the linker will complain of multiple definitions.