conditional (SFINAE) override

坚强是说给别人听的谎言 提交于 2019-11-28 03:22:48

问题


I'm trying to do this:

struct A
{
  virtual int f() const { return 0; }
};

template <typename T>
struct B : A
{
  template <typename U = T,
    typename std::enable_if<...some condition involving U...>::type>
  int f() const { return 1; }
};

Caveat, I can't inherit class templates (use static overrides). Is this sort of construct allowed and can the template member B::f() override the member A::f()?


回答1:


Try this:

template <typename T, typename=void>
struct B : A
{
  ...
};

temlate <typename T>
struct B<T, typename std::enable_if<...some condition...>::type>:
  A
{
  virtual int f() const override { return 1; }
};

where we have two versions of B<T>, one for which the condition is true (the enable_if one), one for which the condition is false (the default one).

If you wanted to be able to reuse your default B implementation, you could even do this:

template <typename T, typename=void>
struct B : A
{
  ...
};

template <typename T>
struct B<T,  typename std::enable_if<...some condition...>::type>:
  B<T, bool>
{
  virtual int f() const override { return 1; }
};

where we inherit from the "false" case in the "true" case. But that is a bit dirty to me -- I'd rather put the common implementation in some third spot (B_impl) rather than that hack. (That also lets you static assert that the second argument is void in the first case of B).



来源:https://stackoverflow.com/questions/14843900/conditional-sfinae-override

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!