是否有任何技巧可以避免在模板类中使用`typename`关键字?

问题描述 投票:0回答:1

我正在尝试实现以下目标:

template <typename T>
class MyClass {
    struct nested {
        using OtherT = // Some type derived from T
    };

protected:
    // Any way to avoid `typename` here?
    typename nested::OtherT member;
};

是否有任何方法可以使用嵌套在struct / namespace / other中的类型,这些类型是根据模板类型T计算出来的,而不需要使用typename关键字?我愿意以任何其他方式声明nested,它的目的只是保留类型。

Edit:我要这样做的原因是,我将在T内部具有从MyClass派生的完整类型集合,然后在这些内部具有相同类型的紧密相关变体nested。我希望使用nested清晰明确地区分它们。这种情况将在代码库的许多地方出现,因此我希望避免在提到这些类型的任何地方都看到typename

c++ templates c++17 dependent-type
1个回答
0
投票

不要将它们放在这样的类型中。如果您需要区分它们,请给他们一个更具描述性的名称。即使您的首选机制有效,仍然必须为名称加上nested::前缀。因此,只需将其作为名称的一部分,然后使用:

template <typename T>
class MyClass {
    using nested_OtherT = // Some type derived from T

protected:
    nested_OtherT member;
};
© www.soinside.com 2019 - 2024. All rights reserved.