2

Why does the following code work (Utilities is a namespace):

template<>
std::map<uint8_t, std::string> A::Utilities::EnumDescription<A::B::Tabs, uint8_t>::descMap =
{...};

while this one does not?

namespace A
{
namespace B
{

    template<>
    std::map<uint8_t, std::string> A::Utilities::EnumDescription<Tabs, uint8_t>::descMap =  // Error --> descMap
    {...};

}
}

Error C2888: symbol cannot be defined within namespace 'B'
Member "A::Utilities::EnumDescription::descMap [with E=A::B::Tabs, T=uint8_t]" cannot be specialized in the current scope

3
  • 1
    Neither posted piece of prose compiles as C++ (nothing to talk of "does work"). Commented Sep 16, 2019 at 13:41
  • {...} is pseudo-code. Commented Sep 16, 2019 at 14:13
  • @Pietro -- it's pseudo-code that doesn't compile. Don't post code that people who want to try it out have to edit. Commented Sep 16, 2019 at 15:01

1 Answer 1

4

Full and partial specialisations of a template X can be defined in the same namespace where X is defined, or in a namespace enclosing (directly or indirectly) that namespace, but cannot be defined in other namespaces (such as child or "cousin" namespaces of X's namespace). That's just the rules of the language.

In your case, the template involved is A::Utilities::EnumDescription, so the specialisation must happen within A::Utilities, A, or the global namespace.

Sign up to request clarification or add additional context in comments.

9 Comments

Shouldn't namespace A { namespace B { Tabs } } and A::B::Tabs be equivalent in this context?
Does the compiler look for Tabs in A::Utilities?
@Pietro The problem is not with Tabs, it's with EnumDescription. That's the template you're specialising, and it's obviously defined in namespace A or A::Utilities (your code doesn't show if that's a NS or a class), so it must be specialised accordingly.
Note the first :: are needed for stuff being fully qualified. You can have Utilities sub-namespace in every of namespaces of your project.
@Pietro The compiler may look for it, but if it doesn't find it, it will try ::A::Utilities too. The whole problem with your code is that you cannot specialise A::something inside A::B.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.