本文介绍了enable_if类型不是特定的模板类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

TLDR:请参阅最后一段.

TLDR: See the last paragraph.

我为几个模板类定义了一个operator&,如下所示:

I have an operator& defined for several template classes like so:

template <typename T>
struct Class {
    Class(T const &t) { }
};

template <typename T_Lhs, typename T_Rhs>
struct ClassAnd {
    ClassAnd(T_Lhs const &lhs, T_Rhs const &rhs) { }
};

template <typename T, typename T_Rhs>
ClassAnd<Class<T>, T_Rhs> operator&(Class<T> const &lhs, T_Rhs const &rhs) {
    return ClassAnd<Class<T>, T_Rhs>(lhs, rhs);
}

template <typename T0, typename T1, typename T_Rhs>
ClassAnd<ClassAnd<T0, T1>, T_Rhs> operator&(ClassAnd<T0, T1> const &lhs, T_Rhs const &rhs) {
    return ClassAnd<ClassAnd<T0, T1>, T_Rhs>(lhs, rhs);
}

int main() {
    Class<int> a(42);
    Class<double> b(3.14);
    auto c = a & b;
}

这很好用.

当我要添加一个not操作时会出现问题,该操作只能在and操作的一侧或另一侧使用,并且必须返回ClassAndNot而不是ClassAnd的实例:

The problem occurs when I want to add a not operation, which is allowed on only one side or the other of an and operation, and must return an instance of ClassAndNot rather than ClassAnd:

template <typename T>
struct ClassNot {
    ClassNot(T const &t) : value(t) { }
    T value;
};

template <typename T_Lhs, typename T_Rhs>
struct ClassAndNot {
    ClassAndNot(T_Lhs const &lhs, T_Rhs const &rhs) { }
};

template <typename T_Lhs, typename T_Rhs>
ClassAndNot<T_Lhs, T_Rhs> operator&(T_Lhs const &lhs, ClassNot<T_Rhs> const &rhs) {
    return ClassAndNot<T_Lhs, T_Rhs>(lhs, rhs.value);
}

template <typename T_Rhs>
ClassNot<T> operator!(T_Rhs const &rhs) {
    return ClassNot<T_Rhs>(rhs);
}

...

auto c = a & !b;

这会导致operator&用任意右侧返回ClassAndoperator&ClassNot右侧返回ClassAndNot之间存在歧义.

This results in an ambiguity between the operator& taking an arbitrary right hand side to return a ClassAnd, and the operator& taking a ClassNot right hand side to return a ClassAndNot.

问题:

如果第一个operator&的右侧是任何ClassNot类型,如何在这里使用std::enable_if禁用它?如果一侧是另一侧的模板实例,是否有类似std::is_same的示例返回true?

How could std::enable_if be used here to disable the first operator& if its right hand side is of any of the types ClassNot? Is there something like std::is_same that returns true if one side is a template instance of the other?

p.s.您可以在 ideone 上找到完整的工作示例.

p.s. You can find a full working example on ideone.

推荐答案

您应该能够为此构建自己的特征:

You should be able to construct your own trait for this:

template <class T>
struct IsClassNot : std::false_type
{};

template <class T>
struct IsClassNot<ClassNot<T>> : std::true_type
{};


template <typename T, typename T_Rhs>
typename std::enable_if<!IsClassNot<T_Rhs>::value,
ClassAnd<Class<T>, T_Rhs>>::type operator&(Class<T> const &lhs, T_Rhs const &rhs) {
    return ClassAnd<Class<T>, T_Rhs>(lhs, rhs);
}

实时示例

当然,您可以为泛化而疯狂,并创建通用的特征:

Of course, you can go crazy with generalisations and create an all-purpose trait:

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

template <template <class... > class TT, class... A>
struct is_instantiation_of<TT<A...>, TT> : std::true_type
{};

template <class T>
using IsClassNot = is_instantiation_of<T, ClassNot>;

实时示例

这篇关于enable_if类型不是特定的模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 10:00