Template specializations and namespaces

J

Juha Nieminen

I'm wondering why this seems to be illegal C++:

namespace Hops { template<bool> void foo(); }
template<> void Hops::foo<false>() { std::cout << "false\n"; }
template<> void Hops::foo<true>() { std::cout << "true\n"; }

Both gcc and clang give errors, saying that the template specializations
are in a "different namespace" than the template declaration.

(Yes, I know that the problem can be solved by putting the
specializations explicitly inside the namespace. However, this seems
to be an odd special rule. Compilers do not seem to have any problem
in compiling this if you enable a proper extension. Why does this rule
exist?)
 
A

Andrew Tomazos

  I'm wondering why this seems to be illegal C++:

namespace Hops { template<bool> void foo(); }
template<> void Hops::foo<false>() { std::cout << "false\n"; }
template<> void Hops::foo<true>() { std::cout << "true\n"; }

  Both gcc and clang give errors, saying that the template specializations
are in a "different namespace" than the template declaration.

  (Yes, I know that the problem can be solved by putting the
specializations explicitly inside the namespace. However, this seems
to be an odd special rule. Compilers do not seem to have any problem
in compiling this if you enable a proper extension. Why does this rule
exist?)

http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top