Tiene que usar el 0de tipo variant::types
. Esto le da una secuencia compatible con MPL que luego podemos usar con mpl::at
y una plantilla para hacer nuestra oferta. Esto hace el truco:
#include <string>
#include <boost/variant.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/int.hpp>
template<typename U, typename V>
void construct_in(V& v) {
v = U();
// modern
// v = U{};
}
int main()
{
typedef boost::variant<int, std::string> variant;
typedef boost::mpl::at<variant::types, boost::mpl::int_<1>>::type pos;
variant v;
// use type deduction
construct_in<pos>(v);
// does not throw, does work
std::string& s =boost::get<std::string>(v);
return 0;
}
Aquí va el tiempo de ejecución de la variante:
#include <string>
#include <vector>
#include <functional>
#include <boost/variant.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/for_each.hpp>
typedef boost::variant<int, std::string> variant;
typedef variant::types types;
typedef std::vector< std::function<void(variant&)> > fvec;
template<typename U, typename V>
void construct_in(V& v) {
v = U{};
}
struct build_and_add {
fvec* funcs;
template<typename T>
void operator()(T) {
funcs->push_back(&construct_in<T, variant>);
}
};
int main()
{
variant v;
std::vector< std::function<void(variant&)> > funcs;
// cannot use a lambda, would need to be polymorphic
build_and_add f = {&funcs};
boost::mpl::for_each<types>(f);
// this is runtime!
int i = 1;
funcs[i](v);
// does not throw, does work
std::string& s =boost::get<std::string>(v);
return 0;
}
Es un poco arcano y necesitará algunos ajustes con variadic argumentos para ser verdaderamente genérico, pero hace lo que quiere . Alguien más necesita averiguar si esto da como resultado una explosión significativa del código .
Como menciona MPL, creo que 'N' (el índice de tipo) es conocido en tiempo de compilación, sin embargo, la deserialización lenta sugiere que solo estará disponible en tiempo de ejecución -> ¿cuál es? –
@MatthieuM. Esto sería imposible con el último. Espero lo primero, de lo contrario mi respuesta tampoco tiene valor. – pmr
@MatthieuM. Ah, buen punto: solo está disponible en tiempo de ejecución. – James