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

Dan*_*nte 4 c++ metaprogramming sfinae c++98

我正在尝试做一个我学校的旧项目,它涉及C++ 98中的元编程.我正在努力反对的部分是关于SFINAE.

主题说我应该operator<<通过使用这样的结构检查流对象和另一个对象之间是否有效:

template<typename Stream, typename Object>
struct IsPrintable;
Run Code Online (Sandbox Code Playgroud)

它说我应该用"两个空引用"写一个奇怪的行,我想它应该是这样的:

sizeof(*(static_cast<Stream *>(NULL)) << *(static_cast<Object *>(NULL)))
Run Code Online (Sandbox Code Playgroud)

它在支持运算符时有效,但在不支持时不运行.我无法弄清楚我失败的地方,这里是文件:

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

};
Run Code Online (Sandbox Code Playgroud)

主题明确地说使用以size_t作为参数的类,并且isPrintable方法应该采用指向此类实例的NULL指针.另外,使用static_cast的丑陋表达式应该用于类型定义,我试图键入它但是编译器对我尖叫.

我不知道所有内容,因为我对此非常陌生,我知道有一些方法可以简化decltype操作符,但项目的目的是在C++ 98中完成,如果有的话,它可能很有用.我稍后会找到一些这种类型的代码.

Pio*_*cki 6

#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);
};
Run Code Online (Sandbox Code Playgroud)

DEMO