I have a class with several template methods, i.e.:
class MessageEndpoint
{
public:
using SupportedMessages = boost::mp11::mp_list<messaging::MessageType1, messaging::MessageType2, messaging::MessageTypeN>;
public:
virtual ~MessageEndpoint();
public:
template <typename MessageType>
UID subscribe(const UID &publisher_uid, std::function<void(const MessageType &)> callback);
template <typename MessageType>
void send_message(const MessageType &message);
template <typename MessageType>
void send_message(MessageType &&message);
}
These methods must be preinstantiated for the several types (in the SupportedMessages list).
Of course, I can do something like this for every class in the list:
// Instantiation.
template <>
UID subscribe<messaging::MessageType1>(const UID &publisher_uid, std::function<void(const messaging::SpikeMessage &)> callback);
template <>
void MessageEndpoint::send_message<messaging::MessageType1>(messaging::MessageType1 &&message);
template <>
void MessageEndpoint::send_message<messaging::MessageType1>(const messaging::MessageType1 &message);
But this is long and ugly. How can I do something, like this:
template_for<MessageEndpoint::SupportedMessages, T,
{
template <>
UID subscribe<T>(const UID &publisher_uid, std::function<void(const T &)> callback);
template <>
void MessageEndpoint::send_message<T>(T &&message);
template <>
void MessageEndpoint::send_message<T>(const T &message);
}>
Is it possible? How can I do this with Boost::MP11?
I've found an acceptable solution.
Some partially solutions (thanks to @davis-herring and @quxflux):
Conditions:
All information about instantiation must be known at compile time. There is no C++ language construct for creating an "instantiation cycle".
So there is only one way to solve this problem: use a preprocessor. Convenient way is to use Boost.Preprocessor library.
I'll show simplified example: Entity-Relation base implementation. This example probably won't compile, but the real code it's based on, works.
Implementation:
This code is enough for me, but if somebody wants, he can make more complicated stuff, using this "technique". For example,
BOOST_PP_SEQ_FOR_EACH_PRODUCTcan be used to make all combinations of several classes list (some problems must will be solved, i.e. equal classes combinations instantiation several times, but this is possible).