enable class's member depending on template

会有一股神秘感。 提交于 2019-12-21 03:52:21

问题


I already know that you can enable (or not) a class's method using std::enable_if

for exemple:

template<size_t D, size_t E>
class Field
{
  ...

  size_t offset(const std::array<float,D>& p) const
  {
    ...
  }

  template<typename TT = size_t>
  typename std::enable_if<D!=E, TT>::type
  offset(const std::array<float,E>& p) const
  {
    return offset(_projection(p));
  }

  ...
};

This helps not being able to call function that are invalid in a specific case as well as removing overloading errors ... which, to me, is very nice !

I'd like to go further and make some of my class's members being present only if the are needed. That way I would get an error if I try to use an objected which would have otherwise not been initiated

I tried to do

template<size_t D, size_t E>
class Field
{
  ...

  template<typename TT = projectionFunc>
  typename std::enable_if<D!=E, TT>::type _projection;
}

But the compiler tells me :

erreur: data member ‘_projection’ cannot be a member template

Is there any way to achieve what I want ?


回答1:


Hold the data members in a separate class that you can then specialize as needed.

template<size_t D, size_t E>
class Field {
    template<size_t, size_t> struct Field_Members {
        int _projection;
    };
    template<size_t V> struct Field_Members<V, V> { };
    Field_Members<D, E> m;
};

and then use m._projection etc.

Field_Members doesn't have to be a nested class template; you can move it outside if desired. It is also possible to have Field inherit from it, but then it'd be a dependent base, and you'd have to write this->_projection, so it doesn't save much typing.




回答2:


AFAIK, this is not possible with a simple SFINAE inside the class template. You can, of course, have the type of the member dependent on a compile-time condition, i.e. via std::conditional, but not eliminate the member entirely.

What you can do, of course, is use another class template, such as

template<bool Condition, typename T> struct Has { T value; };
template<typename T> struct Has<false,T> {};

and declare a member (or base) of this type and access the object via Has<>::value:

typename<Condition>
class foo
{
  Has<Condition, double> x;   // use x.value (only if Condition==true)
};


来源:https://stackoverflow.com/questions/28930916/enable-classs-member-depending-on-template

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