Question

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

Was it helpful?

Solution 2

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)))();
}

OTHER TIPS

The pedestrian way is to define each possible option separately:

template <typename T> T get(Aggregate &);    // leave undefined

template <> uint32_t get(Aggregate & a) { return a.asInt(); }

// ...

Absent any more systematic structure that encodes which function serves which conversion, I think this is the best you can do. It may be worth redefining Aggregate, though, to be more introspectible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top