gpt4 book ai didi

c++ - 命名空间范围问题

转载 作者:搜寻专家 更新时间:2023-10-31 00:50:17 27 4
gpt4 key购买 nike

为什么下面的代码可以工作(Utilities 是一个命名空间):

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

而这个不是?

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

最佳答案

模板的完全和部分特化 X可以在与 X 相同的命名空间中定义已定义,或在包含(直接或间接)该命名空间的命名空间中,但不能在其他命名空间(例如 X 的命名空间的子命名空间或“表兄弟”命名空间)中定义。这只是语言规则。

在您的例子中,涉及的模板是 A::Utilities::EnumDescription , 所以特化必须在 A::Utilities 内发生, A ,或全局命名空间。

关于c++ - 命名空间范围问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57958050/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com