简体   繁体   English

enable_if类型不是某个模板类

[英]enable_if type is not of a certain template class

TLDR: See the last paragraph. TLDR:见最后一段。

I have an operator& defined for several template classes like so: 我有一个operator&为几个模板类定义,如下所示:

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;
}

This works just fine. 这很好用。

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 : 当我想添加一个not操作时,问题就出现了,这个操作只允许在一个或另一个操作上,并且必须返回ClassAndNot而不是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;

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&任意右手边返回ClassAnd之间的歧义,以及operator& ClassNot右手边返回ClassAndNot


Question: 题:

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 ? 如何在这里使用std::enable_if来禁用第一个operator&如果它的右侧是ClassNot的任何类型? Is there something like std::is_same that returns true if one side is a template instance of the other? 是否有类似std::is_same东西,如果一方是另一方的模板实例,则返回true?

ps You can find a full working example on ideone . ps你可以在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);
}

Live example 实例


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>;

Live example 实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM