In c++11 I have very neat and working code for extracting std::tuple
item by type (As I know this feature even placed to c++14 stl)
Now I'm facing with the task to select item by the inherited class specification
struct A
{
int a;
};
struct B : public A
{
int b;
};
...
auto tval = std::make_tuple(1, B());
//now I would like to reference items as following:
tuple_ref_by_inheritance<A>(tval).a = 5; //Access to B instance by parent A
Following code is my unsuccessful try:
template< class T, class Tuple >
struct tuple_ref_index;
// recursive case
template<class T, class Head, class... Tail >
struct tuple_ref_index<T, std::tuple<Head, Tail...> >
{
enum { value = tuple_ref_index<T, std::tuple<Tail...>>::value + 1 };
};
template<class T, class Head, class... Tail >
struct tuple_ref_index<T, std::tuple<Head, Tail...> >
{
const static typename std::enable_if<
std::is_same<T, Head>::value>::type* _= nullptr;
enum { value = 0 };
};
template <class T, class Tuple>
inline T& tuple_ref_by_inheritance(Tuple& tuple)
{
return std::get< tuple_ref_index<T, Tuple>::value >(tuple);
}
1
? Do you anticipate having more than one derived class in the tuple that you wish to select from? – Rosiarosicrucian