Given a set of functions, such as:
template<class A1>
Void Go(A1 a);
template<class A1, class A2> Void Go(A1 a1, A2 a2);
template<class A1, class A2, class A3> Void Go(A1 a1, A2 a2, A3 a3); Is it possible to take an array of some variant type and given its contents, fire the correct function? My application for this is that I want to pass a set of parameters X, to another process, where I only have the option of passing a single pointer. My idea was to send a pointer to a std::vector<boost::any> and then to somehow work out which of the above methods to fire given its contents.
This concerns my experiments with cross-thread eventing and communication, hence it may seem unnecessarily esoteric!
Edit: ok, for example, this is the intention. Obviously it doesn't compile (the template resolution occurs at compile-time, but I want to determine which function to call at run-time!):
#include <boost\any.hpp>
#include <vector>
#include <iostream>
#include <string>
class A
{
public:
void Go()
{
std::cout << L"(0 params)\n";
}
template
void Go(U0 u0)
{
std::cout << L"1 param " << u0 << L"\n";
}
template
void Go(U0 u0, U1 u1)
{
std::cout << L"2 params " << u0 << L" " << u1 << L"\n";
}
template
void Go(U0 u0, U1 u1, U2 u2)
{
std::cout << L"3 params " << u0 << L" " << u1 << L" " << u2 << L"\n";
}
};
class B
{
public:
void Whatever() {}
};
int main(int argc, wchar_t* argv[])
{
// Create a collection of variants.
std::vector<boost::any> myVariants;
B myB;
myVariants.push_back(123);
myVariants.push_back(std::wstring(L"Test"));
myVariants.push_back(&myB);
// Take a void pointer to them.
void *variants = &myVariants;
// Convert back into an array.
std::vector<boost::any>& myConverted = *(std::vector<boost::any> *)(variants);
// Fire the correct event on A.
A myA;
switch(myConverted.size())
{
case 0:
myA.Go();
break;
case 1:
myA.Go(myConverted[0]);
break;
case 2:
myA.Go(myConverted[0], myConverted[1]);
break;
case 3:
myA.Go(myConverted[0], myConverted[1], myConverted[2]);
break;
default: ;
// throw
}
}