Can I use boost::enable_if on a member function?

后端 未结 1 965
旧时难觅i
旧时难觅i 2021-01-18 07:28

I\'m writing a template class, and I want to allow an additional method to exist only for a certain template type. Currently the method exists for all template types, but c

相关标签:
1条回答
  • 2021-01-18 07:52

    Yes, this is possible, but not with the class template parameter directly. boost::enable_if can only be used with a template parameter on the method itself. So, with a little typedef usage:

    template<typename T, typename BASE>
    class MyClass  : public BASE
    {
    public:
      typedef Utility2<BASE> util;
    
      typename T& operator() (const Utility1<BASE>& foo);
    
      template<typename U>
      typename boost::enable_if<boost::is_same<util, U>, T>::type const &
      operator() (const U& foo) const;
    };
    

    This works, because Utility2 can only be created from a certain BASE type. So if the BASE type is something else, the const version of operator() won't exist.

    So, it's a very minor thing. It doesn't gain me much. But it was neat to do.

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题