Here is some code which does not compile.
namespace ns { class foo { template <typename T> int bar (T *); }; } template <typename T> int ns :: foo :: bar (T*) // this is OK { return 0; } template <> int ns :: foo :: bar <int> (int *) // this is an error { return 1; }
The error is: "specialisation of ‘template int ns::foo::bar(T*)’ in different namespace [-fpermissive] from definition of ‘template int ns::foo::bar(T*)"
Here is a version which does compile:
namespace ns { class foo { template <typename T> int bar (T *); }; } template <typename T> int ns :: foo :: bar (T*) { return 0; } namespace ns { template <> int foo :: bar <int> (int *) { return 1; } }
Why does the second definition have to be in a namespace ns {}
block when the first one is quite happily defined with a qualified name? Is it just an oversight in the language design or is there a reason for this?