【问题标题】:C++ Type traits in constructor causing error [duplicate]构造函数中的C ++类型特征导致错误[重复]
【发布时间】:2012-09-11 12:53:53
【问题描述】:

可能重复:
Where and why do I have to put the “template” and “typename” keywords?

我希望有一个构造函数,它接受单个参数,并且仅当该参数的类型具有成员类型 ::t 时才启用,该成员类型必须是其他类型的子类型。我为此使用了类型特征,代码如下所示:

#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

我做错了什么?

【问题讨论】:

    标签: c++ sfinae typetraits enable-if


    【解决方案1】:

    您必须将typename 添加到std::enable_if&lt;...&gt;::type 才能解决此问题...

    【讨论】:

    • 哦,对了,因为 ::type 依赖于模板参数,所以我必须使用 typename 前缀,对吧?
    • 没错。我想,分析类型中的另一个间接级别(=T::t)也会导致问题。
    • 为什么?我在这里使用了 typename 关键字。
    猜你喜欢
    • 2017-02-19
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    • 2011-06-24
    • 1970-01-01
    相关资源
    最近更新 更多