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

后端 未结 1 980
旧时难觅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
    class MyClass  : public BASE
    {
    public:
      typedef Utility2 util;
    
      typename T& operator() (const Utility1& foo);
    
      template
      typename boost::enable_if, 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)
提交回复
热议问题