C++类型特征在构造函数中引起错误

3
我希望有一个构造函数,它只接受一个参数,并且仅在该参数的类型具有成员类型::t且必须是另一种类型的子类型时才启用。我使用类型特征来实现这一点,代码如下:

可能重复:
在哪里以及为什么我必须放置“template”和“typename”关键字?

代码如下:

#include <type_traits>

struct Y{};

struct X{
    //Only allow if T has a type member T::t which is a subtype of Y
    template <typename T>
    X(T* t, std::enable_if<std::is_base_of<Y, typename T::t>::value, int>::type e = 0){}
};

然而,g++抱怨以下内容:

test/test.cpp:8:75: error: ‘std::enable_if<std::is_base_of<Y, typename T::t>::value, int>::type’ is not a type

我做错了什么?

1个回答

5

为了解决这个问题,您需要向 std::enable_if<...>::type 添加一个 typename


哦,对了,由于 ::type 依赖于模板参数,我必须使用 typename 前缀,是吗? - gexicide
没错。我想分析类型中的另一个间接级别(= T::t)也会导致问题。 - Paul Michalik
为什么?我在这里使用了typename关键字。 - gexicide

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接