I have a following code:
namespace A
{
template <typename T>
void func(T);
namespace B
{
class foo {};
template <>
void ::A::func(foo); // ERROR
class bar {};
class foobar {};
}
}
I have a A::func<T>
template function and want to make specialization for A::B::foo
class and want to make it right under the definition of class A::B::foo
to make it easier to navigate and read code. Now I am getting an error Cannot define or redeclare 'func' here because namespace 'B' does not enclose namespace 'A'
. Can I define my specialization somewhere near to A::B::foo
?
I see the only way to close namespace A::B
, define specialization and open namespace again to define bar
and foobar
, but it does not look very good to me:
namespace A
{
template <typename T>
void func(T);
namespace B {
class foo {};
}
template <>
void ::A::func(B::foo);
namespace B {
class bar {};
class foobar {};
}
}
You can’t do this: there are no rules for how name lookup would work in a situation where you were inside namespace B
lexically but inside A
logically (via func
). (Note that you don’t need the ::A::
in the working version.) There has been at least one proposal to allow this, and it might be revived at some point, but those rules would have to be written.