Search code examples
c++templatesgeneric-programming

Select function name based on template parameter


Is there a way to automatically select between multiple non-template functions based on a template parameter?

Example:

class Aggregate
{
public:
     std::string asString();
     uint32_t asInt();
private:
     // some conglomerate data
};

template <typename T>
T get(Aggregate& aggregate)
{
     // possible map between types and functions?
     return bind(aggregate, typeConvert[T])(); ??
     // or
     return aggregate.APPROPRIATE_TYPE_CONVERSION();
}

The solution would be nice to throw a compiler error if there is no good conversion available, i.e.

get<double>(aggregate); // compile error

I do not want to use template specialization, i.e

template<>
int get(Aggregate& aggregate)
{
    return aggregate.asInt();
}

because it leads to code duplication when your get() function has more then one line of code


Solution

  • You may do something like (require C++11) : (https://ideone.com/UXrQFm)

    template <typename T, typename... Ts> struct get_index;
    
    template <typename T, typename... Ts>
    struct get_index<T, T, Ts...> : std::integral_constant<std::size_t, 0> {};
    
    template <typename T, typename Tail, typename... Ts>
    struct get_index<T, Tail, Ts...> :
        std::integral_constant<std::size_t, 1 + get_index<T, Ts...>::value> {};
    
    template <typename T, typename Tuple> struct get_index_in_tuple;
    
    template <typename T, typename ... Ts>
    struct get_index_in_tuple<T, std::tuple<Ts...>> : get_index<T, Ts...> {};
    
    
    class Aggregate
    {
    public:
         std::string asString();
         uint32_t asInt();
    private:
         // some conglomerate data
    };
    
    template <typename T>
    T get(Aggregate& aggregate)
    {
        using types = std::tuple<uint32_t, std::string>;
        auto funcs = std::make_tuple(&Aggregate::asInt, &Aggregate::asString);
    
        return (aggregate.* (std::get<get_index_in_tuple<T, types>::value>(funcs)))();
    }