【问题标题】:C++ dependent name: Is this typename required?C++ 依赖名称:此类型名称是否必需?
【发布时间】:2016-11-23 05:26:29
【问题描述】:

a.hpp我定义:

#include <utility>
namespace Board {
    template<int W, int H>
    struct GroupNode
    {
        using PointType = std::pair<int, int>;
        // ...
    };
}

然后,在b.cpp我定义:

#include "a.hpp"
namespace Board {
    template<int W, int H>
    struct NodeList
    {
        using StdList = std::list < /* typename */ GroupNode<W, H>>;
    }
}
// and then use NodeList<19, 19> nl;

上面的代码可以在 gcc-6 和 clang-3.9 上编译而没有任何警告。 然而,Clion 2016.3 在b.cpp 中抱怨cannot resolve variable GroupNode。取消注释 typename 可以驯服 Clion 警告,但我想知道这 typename 是否是必需的?如果是这样,为什么 g++/clang++ 没有发出任何警告?

【问题讨论】:

    标签: c++ templates language-lawyer dependent-name


    【解决方案1】:

    不,这不是必需的。根据 C++14 中的 [temp.res]/3:

    qualified-id 旨在引用不是当前实例化成员的类型时 (14.6.2.1) 并且它的nested-name-specifier是一个依赖类型,它应该以关键字typename为前缀,形成 一个 typename-specifier 。如果 typename-specifier 中的 qualified-id 不表示类型,则程序是 ill- 形成。

    这里没有 nested-name-specifier 引用依赖类型,因此不需要typename。 (nested-name-specifier 指的是 :: 及其左侧的类型或命名空间。显然,std 不是类型,更不用说依赖类型了。)

    【讨论】:

      猜你喜欢
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-20
      相关资源
      最近更新 更多