首页 > 解决方案 > 为什么 C++“使用 XXX = type”类型别名需要“typename”?

问题描述

声明类型别名时,

using T = XXX;

XXX 只能是一个类型。

如果 XXX 是模板类内部的类型别名,为什么还需要在 XXX 之前包含“typename”?

例如,

template <typename T> struct fred
{
    using T2 = T[2];
};

template <typename U> struct mary
{
    // why is typename necessary here?
    using M2 = typename fred <U>::T2;
    // this does not compile, even though type alias must refer to a type
    // using M2 = fred <U>::T2;
};

标签: c++typesusingtypename

解决方案


推荐阅读