I'm trying to figure out how the template method generation works inside a class. I've found code that describes the functionality which I would like to use but cannot understand how are the methods being generated. Desired functionality is the automatic creation of methods that are templated based on the std::tuple arguments. Code snippet was taken from: https://commschamp.github.io/comms_protocols_cpp/ Here's the code:
template <typename TCommon, typename TAll>
class GenericHandler;
template <typename TCommon, typename TFirst, typename... TRest>
class GenericHandler<TCommon, std::tuple<TFirst, TRest...> > :
public GenericHandler<TCommon, std::tuple<TRest...> >
{
using Base = GenericHandler<TCommon, std::tuple<TRest...> >;
public:
using Base::handle; // Don't hide all handle() functions from base classes
virtual void handle(TFirst& msg)
{
// By default call handle(TCommon&)
this->handle(static_cast<TCommon&>(msg));
}
};
template <typename TCommon>
class GenericHandler<TCommon, std::tuple<> >
{
public:
virtual ~GenericHandler() {}
virtual void handle(TCommon&)
{
// Nothing to do
}
};
Example of usage:
class Message
{
public:
Message() = default;
virtual ~Message() {};
};
class X : public Message
{
public:
X() = default;
virtual ~X() {}
int x;
};
class Y : public Message
{
public:
Y() = default;
virtual ~Y() {}
int y;
};
using AllMessages = std::tuple<X,Y>;
class Handler : public GenericHandler<Message, AllMessages> {};
int main()
{
X x;
Handler h;
h.handle(x);
std::cout<<"Hello World";
return 0;
}
As I understand the template <typename TCommon, typename TFirst, typename... TRest> class GenericHandler<TCommon, std::tuple<TFirst, TRest...> > generates the handle() method and when the std::tuple is empty then the template <typename TCommon> class GenericHandler<TCommon, std::tuple<> > is used.
How does using Base::handle; work in this example?
How does GenericHandler inheritance work in this case template <typename TCommon, typename TFirst, typename... TRest> class GenericHandler<TCommon, std::tuple<TFirst, TRest...> > : public GenericHandler<TCommon, std::tuple<TRest...> >?
What is the process of generating the template methods?
Edit: I'm bound to C++ 11, but I'm interested in what can be done on the newer versions of C++.