Checking whether a function (not a method) exists in c++11 via templates

前端 未结 2 1520
猫巷女王i
猫巷女王i 2021-01-13 13:52

So with SFINAE and c++11, it is possible to implement two different template functions based on whether one of the template parameters can be substituted.

For exampl

2条回答
  •  攒了一身酷
    2021-01-13 14:47

    Using SFINAE, it's possible like this:

    template 
    class has_ostream_lshift
    {
      struct no {};
    
      template 
      static decltype(std::declval() << std::declval()) test(int);
    
      template 
      static no test(...);
    
    public:
      enum { value = ! std::is_same(0))>::value};
    };
    

提交回复
热议问题