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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top