简体   繁体   English

SFINAE检查运营商的存在(没有decltype)

[英]SFINAE to check the existence of operators (without decltype)

I am trying to do an old project of my school, which deals with metaprogramming in C++98. 我正在尝试做一个我学校的旧项目,它涉及C ++ 98中的元编程。 The part I'm struggling against is about SFINAE. 我正在努力反对的部分是关于SFINAE。

The subject says I'm supposed to check if operator<< works between a stream object and another object by the use of a struct like this : 主题说我应该通过使用这样的结构来检查operator<<是否在流对象和另一个对象之间起作用:

template<typename Stream, typename Object>
struct IsPrintable;

It says I should write a weird line with "two null references", I guess it should look like this : 它说我应该用“两个空引用”写一个奇怪的行,我想它应该是这样的:

sizeof(*(static_cast<Stream *>(NULL)) << *(static_cast<Object *>(NULL)))

It works when the operator is supported, but doesn't compile when it isn't. 它在支持运算符时有效,但在不支持时不运行。 I can't figure out where I fail, here is the file : 我无法弄清楚我失败的地方,这是文件:

template<typename Flux, typename Object>                                                                                                                                                                                                       
struct IsPrintable
{
  typedef char yes[1];
  typedef char no[2];

  template<size_t N>
  struct Test
  {
    typedef size_t type;
  };  

  template<typename U>
  static yes &isPrintable(U * = 0); 

  template<typename>
  static no &isPrintable(...);

  static const bool value = sizeof(isPrintable<Test<sizeof(*(static_cast<Flux *>(NULL)) << *(static_cast<Object *>(NULL)))> >(0)) == sizeof(yes);

};

The subject says explicitly to use a class taking a size_t as a parameter, and that the isPrintable method should take a NULL pointer to this class instance. 主题明确地说使用以size_t作为参数的类,并且isPrintable方法应该采用指向此类实例的NULL指针。 Plus, the ugly expressions with static_cast should be used for a type definition, I tried to typedef it but the compiler screamed at me. 另外,使用static_cast的丑陋表达式应该用于类型定义,我试图键入它但是编译器对我尖叫。

I don't get everything since I'm very new to this, I know there is some way to simplify that with the decltype operator, but the aim of the project is to do it in C++98, and it could be useful if I find some code of that type later on. 我不知道所有内容,因为我对此很新,我知道有一些方法可以使用decltype运算符来简化它,但项目的目的是在C ++ 98中完成它,它可能很有用如果我稍后找到那种类型的代码。

#include <cstddef>

template<typename Flux, typename Object>                                                                                                                                                                                                       
struct IsPrintable
{
    typedef char yes[1];
    typedef char no[2];

    template <std::size_t N>
    struct SFINAE {};

    template <typename F, typename O>
    static yes& isPrintable(SFINAE<sizeof( *static_cast<F*>(NULL) << *static_cast<O*>(NULL) )>* = 0); 

    template <typename F, typename O>
    static no& isPrintable(...);

    static const bool value = sizeof(isPrintable<Flux, Object>(NULL)) == sizeof(yes);
};

DEMO DEMO

template <typename Flux, typename Object>
struct IsPrintable
{
private:
    typedef char yes[1];
    typedef char no[2];

    template <size_t N> struct Test
    {
        typedef size_t type;
    };

    template <typename U>
    static yes& isPrintable(Test<sizeof(*(static_cast<Flux*>(NULL)) << *(static_cast<U*>(NULL)))>* = 0);

    template <typename> static no& isPrintable(...);
public:
    static const bool value = sizeof(isPrintable<Object>(0)) == sizeof(yes);
};

Live example 实例

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

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