Question

When I try to build this code:

// foo.h
namespace foo {
    namespace bar {
        void put();
    }
}
#include "foo.h"
namespace foo {
    namespace {
        template<typename T>
        void put() { }
    }    
    void bar::put() {
        put<int>();
    };
}

I get the error:

foo.cpp: In function ‘void foo::bar::put()’:
foo.cpp: error: expected primary-expression before ‘int’
foo.cpp: error: expected ‘;’ before ‘int’

Clearly, put<int> is using put to refer to bar::put. How can I make it refer to the put<T> in the anonymous namespace?

Was it helpful?

Solution

You could fully qualify the function template's name:

namespace foo {
    namespace bar {
        void put();
    }
}

namespace foo {
    namespace {
        template<typename T>
        void put() { }
    }
    void bar::put() {
        ::foo::put<int>();
    }
}

Also notice, that you don't need to use the semi-colon after a function definition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top