C ++ namespace resolution

When I try to create 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 an error message :

 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> uses put to mean bar::put . How can I make it refer to put<T> in an anonymous namespace?

+4
source share
1 answer

You can fully define the name of the function template:

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

Also note that you do not need to use a comma after defining a function.

+4
source

All Articles