Alias the return type of a const overloaded function

503 views Asked by At

I have the following overloaded function :

template<size_t N, typename T>
auto get(const T & _t) -> decltype(std::get<...>(_t)) {
    ...
}

template<size_t N, typename T>
auto get(T & _t) -> decltype(std::get<...>(_t)) {
    ...
}

First question is :

does the first one uses std::get(const tuple<_Elements...>& __t) and the second one std::get(tuple<_Elements...>& __t) ??

now I want to alias the return type of my new function get :

using type = typename decltype(aux::get<I>(data))::type;

which one is used here ? the const or not ? and How can I choose ? I would like to alias both !! data is non-const

1

There are 1 answers

1
Angew is no longer proud of SO On BEST ANSWER

Yes, the first one uses the const overload and the second one the non-const one. That's because _t is const in the first case and non-const in the second one.

Which one is used in the type alias depends on the type of data. Is it const? If so, the const overload is aliased. If not, the non-const one is.

To get a "virtual value" of any type, you can use std::declval. This code would alias the const version:

using type = typename decltype(aux::get<I>(std::declval<const YourTypeHere>()))::type;