【问题标题】:'dependent name is not a type' when using nested nested class in templated class [duplicate]在模板类中使用嵌套嵌套类时“依赖名称不是类型”[重复]
【发布时间】:2018-03-12 01:19:36
【问题描述】:

有谁知道为什么以下编译失败(在 MSVC2015 中测试)?

template<int N> // when this line is removed it compiles successfully
class A {
    class B {
    public:
        class C {
        };
    };
    void func(B b){} // compiles fine
    void func(B::C c){} // generates warning C4346 and error C2061
};

它在标有注释的行上生成以下内容:

  • warning C4346: 'A&lt;N&gt;::B::C': dependent name is not a type
  • error C2061: syntax error: identifier 'C'

【问题讨论】:

    标签: c++ templates visual-c++ compiler-errors


    【解决方案1】:

    让我们为您搜索它。

    如果要将依赖名称视为类型,则需要 typename 关键字。

    C++ 规则。

    14.6 名称解析

    在模板声明或定义中使用并且依赖模板参数的名称被假定为不命名类型,除非适用的名称查找找到类型名称或名称由关键字typename

    【讨论】:

    • 什么是“从属名称”?
    • 已编辑。见上文。
    • 这个规则有原因吗?
    • @Museful 是的。这是因为访问数据成员或成员类型,甚至成员模板都是一种模棱两可的语法。想象一下foo::bar 是一种类型吗?那是静态数据成员吗?除非有 typename 关键字,否则编译器会假定一个数据成员。
    猜你喜欢
    • 2018-12-08
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多