static_assert引用封闭模板类

时间:2017-11-21 20:35:35

标签: c++ c++11 c++14 c++17 variadic-templates

在下面的代码中,VS2015在IsInstantiation<OtherType, T1>::value处抱怨,给出了此错误“模板参数'TT'的模板参数无效,需要一个类模板”。我该如何解决这个问题?我想将OtherType限制为T1为SomeTypeOtherType的情况。

template <template<typename...> class TT, typename T>
struct IsInstantiation : std::false_type
{
};

template <template<typename...> class TT, typename... Ts>
struct IsInstantiation<TT, TT<Ts...>> : std::true_type
{
};

template <typename T1>
class SomeType{};

template <typename T1, typename T2>
class OtherType
{
    static_assert(IsInstantiation<SomeType, T1>::value ||
        IsInstantiation<OtherType, T1>::value,
        "Event must have SomeType or OtherType as first type");
public:
    explicit OtherType(T1 t1, T2 t2)
        : mT1{ std::move(t1) }
        , mT2{ std::move(t2) }
    {
    }

private:
    T1 mT1;
    T2 mT2;
};

1 个答案:

答案 0 :(得分:2)

尝试

template <typename, typename>
class OtherType;

template <typename T1, typename T2>
using OtherTypeAlias = OtherType<T1, T2>;

template <typename T1, typename T2>
class OtherType
{
    static_assert(IsInstantiation<SomeType, T1>::value ||
        IsInstantiation<OtherTypeAlias, T1>::value,
        "Event must have SomeType or OtherType as first type");

问题:在OtherType内,当您写OtherType时,默认为OtherType<T1, T2>;因此,当您将OtherType作为IsIstantiation的参数传递时,您会传递模板类型,而不是模板模板。

- 编辑 -

我不知道但是可以在OtherType内直接引用干(没有模板默认参数)(感谢Barry!)。

因此更简单

O不知道如何直接引用类OtherType - 作为模板模板,没有默认模板参数 - 在OtherType

的主体内
template <typename T1, typename T2>
class OtherType
{
    static_assert(IsInstantiation<SomeType, T1>::value ||
        IsInstantiation<::OtherType, T1>::value,
        "Event must have SomeType or OtherType as first type");

没有别名

相关问题