Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use C++ 11's using keyword for template function to remove namespace in a scope

Tags:

c++

c++11

I have a template function:

namespace Example
{
   template<class T>
   T Foo() { return T(0); };
}

I would like to use a using statement or similar so that I do not have to prefix the function name with it's namespace when calling it i.e.

template<class T> using Foo = Example::Foo<T>;

However this does not work.

I do not want to use the following approach as it would include everything form the namespace Example:

using namespace Example;

Is there a nice C++ 11 way to create a shortened alias to a function in a namespace?

like image 943
keith Avatar asked Jan 30 '16 19:01

keith


1 Answers

As for any symbol, you can do using Example::Foo;. This can be used either in namespace scope, or in function scope (this is actually present in C++98, it is not new in C++11). The approach you were trying to do works only for types, while Foo is a function.

like image 193
Vladimir Still Avatar answered Oct 06 '22 06:10

Vladimir Still