如何在子类中使用模板基类的嵌套类型?

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

我有这样的代码:

template <typename T>
struct bar
{
        typedef T type;
};

template <typename T>
struct sub_bar : bar<T>
{
        typename bar<T>::type x;
};

我知道这是推荐的方式,但我想编写这样的代码:

template<typename T>

struct sub_bar : bar<T>

{
        ...

        type x;

};

如何做?我想我可以做这样的事情:


template<typename T>

struct sub_bar : bar<T>

{
        typedef typename bar<T>::type type;

        type x;

};

类型是否含糊不清?然而这段代码可以与 gcc9 编译器一起使用。

c++ templates subclass superclass nested-types
1个回答
0
投票

我倾向于使用强制合格查找和写入的技巧

using typename sub_bar::type;

为了避免重复基类的模板参数(并避免在搜索两个类的奇怪情况下潜在的歧义

type
),但你写的绝对有效。

© www.soinside.com 2019 - 2024. All rights reserved.