0

Could anybody explain what this code does?

struct EnumClass
    {
        template <typename T>
        std::size_t operator()(T t) const
        {
            return static_cast<std::size_t>(t);
        }
    };
black sheep
  • 369
  • 4
  • 13
  • Does this answer your question? [What are C++ functors and their uses?](https://stackoverflow.com/questions/356950/what-are-c-functors-and-their-uses) – rezaebrh Jan 06 '20 at 13:03

1 Answers1

6

It defines the operator () for any object of type EnumClass, taking one argument of any type. The operator evaluates to that argument, cast to type size_t.

EnumClass e;
e(1); // evaluates to (size_t)1

This is borderline nonsense, of course. (It might make sense in some other context, but stand-alone and as-is, it doesn't -- you don't need an EnumClass object to cast something to size_t.)

DevSolar
  • 67,862
  • 21
  • 134
  • 209