C 无法解释的类“尚未声明”错误,这是由于名称空间而导致的

c++ Unexplainable class “ has not been declared” error due to namespace

本文关键字:于名 空间 错误 无法解释 未声明 尚未声明      更新时间:2023-10-16

我有一些模板类,有两个私人静态成员。用户定义特征结构并将其提供给模板类,然后从中衍生。

然后在C 文件中,用户定义了静态成员,一个成员从另一个成员开始。 由于某种原因,如果我不完全指定ARG的名称空间,我会得到"尚未声明"错误。这只是一个问题,当我处于嵌套名称空间时,如果您定义单个顶级名称空间中的类型,这没有问题,这使我认为这是一个编译器错误。修剪下面的示例,用GCC 7.2

编译
template<typename Traits>
struct Base 
{
    static int x;
    static int y;
};
namespace foo::bar
{
    struct BarTraits
    {
    };
    using Bar = Base<BarTraits>;
    template<> int Bar::x = 0;
    template<> int Bar::y( Bar::x );  //error 
    //template<> int Bar::y( foo::bar::Bar::x ); //no error
}

根据C 标准temp.expl.spec#3

可以在可以定义相应的主模板的任何范围内声明一个明确的专业化。

您的代码违反了此语句,因为Bar::xBar::y专门用于namespace foo::bar

GCC由于已知的缺陷而错误地接受了前两个专业,https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56119

以下固定代码

template<typename Traits>
struct Base  {
    static int x;
    static int y;
};
struct BarTraits {};
using Bar = Base<BarTraits>;
template<> int Bar::x = 0;
template<> int Bar::y( Bar::x );

被GCC,Clang,MSVC接受:https://gcc.godbolt.org/z/mpxjtzbah

相关文章: