Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the real underlying type of a getter?

I know that getters are in general bad, but here, I just use one to illustrate a more general question.

Consider the following class:

template <class... T>
class my_tuple final
{
    private: 
        std::tuple<T...> _data;
    public: 
        template <class... U> 
        my_tuple(U&&... u) 
        : _data(std::forward<U>(u)...) {} 
    public:
        template <std::size_t I>
        auto get() -> decltype(std::get<I>(_data)) 
        {return std::get<I>(_data);}
};

And consider that I cannot modify this class.

Is there a way, to write an external metafunction my_tuple_type (by external I mean a metafunction not belonging to the class) to actually get the type of the underlying tuple? (I tend to think that it is impossible if one of T... is a reference because just applying std::decay or std::remove_reference on the type returned by get will remove the original reference too).

EDIT: I have added a constructor to help testing.

EDIT2: For clarification, I cannot operate on T...: I am searching for a metafunction only based on the getter.

EDIT3: From the exterior of the class, I do not know the name of the underlying tuple member (here it is named _data, but it could be _tuple or whatever)

EDIT4: As an illustration, this can be achieved if we suppose that none of the types are references/pointers by:

1) Making a metafunction that will execute recursively the getter until it fails (so the tuple size N will be known)

2) Executing a std::decay on each type returned by std::get from 0 to N and putting them together.

But it will fail if one of the tuple element is a reference or pointer...

EDIT5: I will post an implementation of EDIT4 soon (I am working on that)

EDIT6: That is not an XY problem. The fundamental question I try to answer is:

consider a concept called Tuple_like whose only condition is to have a templated get member like here. The question is: from this only function get<I>(), is it possible to extract all information on the underlying tuple ?

like image 531
Vincent Avatar asked Feb 14 '14 23:02

Vincent


1 Answers

No, std::get<I>(some_tuple&) is lossy. It returns the same type for references and value types. If there was a way to have an rvalue qualified call to get you could do it.

Well, there is pattern matching on the my_tuple type itself. And if you knew the name of the std::tuple field (or had a list of all possible names even) there are ways to violate privacy, which might work here. But I suspect those are excluded.

like image 139
Yakk - Adam Nevraumont Avatar answered Oct 21 '22 08:10

Yakk - Adam Nevraumont