是否可以使用SFINAE /模板来检查操作符是否存在? [英] Is it possible to use SFINAE/templates to check if an operator exists?

查看:129
本文介绍了是否可以使用SFINAE /模板来检查操作符是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在编译时检查一个操作符是否存在,如果它不是我想要的忽略,有什么办法吗?



example运算符:

  template< typename T> 
QDataStream& operator<<(QDataStream& s,const QList< T>&


解决方案

我最后使用一个备用命名空间:

 命名空间operators_fallback {
template< typename T>
inline QDataStream&运算符<<(QDataStream& s,const T&){return s; }

template< typename T>
inline QDataStream& operator>>(QDataStream& s,T&){return s; }

template< typename T>
inline QDebug operator<<(QDebug d,const T&){return d; }
};

...
inline void load(QDataStream& s){
using namespace operator_fallback;
s>>项目;
}

同样找到在编译时检查运算符的正确方法



pre> 命名空间private_impl {
typedef char yes;
typedef char(& no)[2];

struct anyx {template< class T> anyx(const T&); };

无操作符<< (const anyx& const,const anyx&);
无操作符>> (const anyx& const,const anyx&);


template< class T>是检查(T const&);
无检查(否);

template< typename StreamType,typename T>
struct has_loading_support {
static StreamType&流;
static T& X;
static const bool value = sizeof(check(stream>> x))== sizeof(yes);
};

template< typename StreamType,typename T>
struct has_saving_support {
static StreamType&流;
static T& X;
static const bool value = sizeof(check(stream<< x))== sizeof(yes);
};

template< typename StreamType,typename T>
struct has_stream_operators {
static const bool can_load = has_loading_support< StreamType,T> :: value;
static const bool can_save = has_saving_support< StreamType,T> :: value;
static const bool value = can_load&&& can_save;
};
}
template< typename T>
struct supports_qdatastream:private_impl :: has_stream_operators< QDataStream,T> {};

template< typename T>
struct can_load:private_impl :: has_loading_support< QDataStream,T> {};

template< typename T>
struct can_save:private_impl :: has_saving_support< QDataStream,T> {};

template< typename T>
struct can_debug:private_impl :: has_saving_support< QDebug,T> {};

//编辑更改了has_stream_operators。



//编辑删除链接,显然网站有一些攻击javascript。


I'm trying to check if an operator exists at compile time, if it doesn't I just want it ignored, is there any way to do that?

example operator:

 template <typename T>
 QDataStream& operator<<(QDataStream& s, const QList<T>& l);

解决方案

I ended up using a fallback namespace :

namespace operators_fallback {
template <typename T>
inline QDataStream& operator<<(QDataStream& s, const T &) { return s; }

template <typename T>
inline QDataStream& operator>>(QDataStream& s, T &) { return s; }

template <typename T>
inline QDebug operator<<(QDebug d, const T &) { return d; }
};

...
inline void load(QDataStream & s) {
    using namespace operator_fallback;
    s >> item;
}

Also found the proper way to check for operators at compile time (although I'm going with the fallback namespace).

more or less based on this :

namespace private_impl {
    typedef char yes;
typedef char (&no)[2];

struct anyx { template <class T> anyx(const T &); };

no operator << (const anyx &, const anyx &);
no operator >> (const anyx &, const anyx &);


template <class T> yes check(T const&);
no check(no);

template <typename StreamType, typename T>
struct has_loading_support {
    static StreamType & stream;
    static T & x;
    static const bool value = sizeof(check(stream >> x)) == sizeof(yes);
};

template <typename StreamType, typename T>
struct has_saving_support {
    static StreamType & stream;
    static T & x;
    static const bool value = sizeof(check(stream << x)) == sizeof(yes);
};

template <typename StreamType, typename T>
struct has_stream_operators {
    static const bool can_load = has_loading_support<StreamType, T>::value;
    static const bool can_save = has_saving_support<StreamType, T>::value;
    static const bool value = can_load && can_save;
};
}
template<typename T>
struct supports_qdatastream : private_impl::has_stream_operators<QDataStream, T> {};

template<typename T>
struct can_load : private_impl::has_loading_support<QDataStream, T> {};

template<typename T>
struct can_save : private_impl::has_saving_support<QDataStream, T> {};

template<typename T>
struct can_debug : private_impl::has_saving_support<QDebug, T> {};

//edit changed has_stream_operators a bit.

//edit removed the link, apparently the site has some attack javascript.

这篇关于是否可以使用SFINAE /模板来检查操作符是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆