jeudi 24 septembre 2020

class containing member function pointer to own member function

I have this situation:

struct S
{
  auto f()
  {
    return [](){};
  }

  template <typename U = S>
  constexpr static auto fp{&U::f};
};


int main()
{
  (S().*S::fp<>)();
}

Where I would like to give the user flexibility to name f() whatever he wants, so a well-known member function pointer is needed, named, for example, my_library_feature_. Is this a good idea, or maybe f itself should have a well-known name? A defined member function pointer can be detected at compile time and would mean, that the class supports a certain feature, that can be used by the library, such as being able to calculate its hash, or being able to serialize itself, ... I know the alternative approach (std::hash<>), but maybe that approach belongs to a different time and I find it cumbersome anyway (possibly having to declare a friend, a separate template specialization). Aren't friends bad style?

Aucun commentaire:

Enregistrer un commentaire