Short version:
I would like to be able to convert a struct to a tuple. At least the type. In the code below, the convertToTuple function does not work, because variadic parameters cannot be used in structured bindings (as far as I know). The key line is: auto& [values...] = value;
struct Vec3 {
float x;
float y;
float z;
};
template <typename T>
auto structToTuple(T &value) {
auto& [values...] = value; //doesn't work
return std::make_tuple<decltype(values)...>(values...);
}
Vec3 v;
std::tuple<float, float, float> t = structToTuple(v);
Basically, what I need is a means to convert the type of a custom structure to a tuple, which contains all of the types from the struct. For example:
struct Ray {Vec3, Vec3} -> std::tuple<
std::tuple<float, float, float>,
std::tuple<float, float, float>>;
Detailed problem:
I would like to create a templated function which takes a type or a list of types as template parameter and produces a list of textures, each texture containing an item. An other function can sample the texture list and pack the values back together to return the same type. For example, if I have a type:
std::pair<std::tuple<int, int>, int> value;
std::tuple<Texture<int, int>, Texture<int>> tex = createTexture(value);
std::pair<std::tuple<int, int>, int> thisshouldwork = sample(tex);
The above code is just a simple example of what I want to do, not my actual code. In this case 2 textures would be created, one texture would contain the two ints from the tuple, the other would hold a single int type. My intention is to hide the texture handling behind an interface, where I can store an arbitrary value (as long as it's composed of some simple types) in textures that I can upload to the GPU for the shaders. As long as I only use std::tuple, and std::pair, it does work, because I can extract the types from them:
template <typename... Args>
void f(std::tuple<Args...> t);
I would like to be able to do the same when the template parameter is a custom struct. For example:
struct Vec3 {
float x;
float y;
float z;
};
Vec3 v;
Texture<float, float, float> tex = createTexture(v);
struct Ray{
Vec3 pos;
Vec3 dir;
};
Ray r;
std::tuple<Texture<float,float,float>, Texture<float,float,float>> tex2 = createTexture(r);
I am not convinced that this is even possibly with the current C++ standard, but based on the structured bindings, it seems possible. My idea would be something like this:
template <typename T>
auto structToTuple(T &value) {
auto& [values...] = value;
return std::make_tuple<decltype(values)...>(values...);
}
Vec3 v;
std::tuple<float, float, float> t = structToTuple(v);
As far as I am concerned variadic parameters only work function or template parameters. But if the structToTuple function would work, that would solve my problem.
Thanks for the help in advance, guys!
Update:
I have found a workaround for my problem (not a general solution): https://github.com/Dwarfobserver/AggregatesToTuples/blob/master/single_include/aggregates_to_tuples.hpp
The author of this library defined a structure to tuple conversion, but it only works if the structure has no more than 50 parameters. This solves my problem in practice, though I am still curious whether it is possible with an arbitrary structure.