I am working on a C++11 wrapper for a variant type implemented in C. The variant type supports common data types like int, float, string, but also tuples. I have converters for the basic types of the form…
template<typename T>
T convert_to(const Variant &var);
… but I am struggling with conversion to std::tuple.
The underlying C API can break out a tuple by returning an array of Variants. It looks something like this:
int get_tuple(Variant var, Variant **argv, int *argc);
Now I recognize I can manually create templates for each size of tuple, but I am looking for a variadic solution that can handle any size of tuple. Any tips on how to approach this?
BTW, the actual thing that I’m trying to wrap is the Erlang NIF API.
Since you’re using C++11 (and you know the tuple type from the template parameter), you can happily use variadic templates. Something like*
where
array_to_tuplewill copy the elements one by one:Hope this can work…
*) Note that such
convert_towill not be easily callable. I suggest doing the specialization on the return types with class templates, because you need partial specialization, which function template cannot have.