我有一个这里描述的表格的类型清单:
http://www.drdobbs.com/generic-programmingtypelists-and-applica/184403813
每种类型都有一个名为get()
的鸭型函数(模板/编译时虚拟),它返回一个简单类型,如下所示:
struct Float {
float get() { return 7.0; }
};
struct Int {
int get() { return 7; }
};
typedef typelist<Float, typelist<Int, null_typelist>> types;
我还有一个函数,它采用可变数量的简单类型参数,如下所示:
template<typename... Args>
foo(Args... args)
{
}
现在我需要一种方法来调用给定foo
的{{1}}。我认为通过一个元组可以解决这个问题,但我真的远不是一个有效的解决方案......
我希望你能在这里帮助我!
答案 0 :(得分:2)
此代码将typelist
转换为tuple
,并使用简单类型调用foo
。
#include <tuple>
#include <iostream>
template<typename H, typename T>
struct typelist
{
typedef H Head;
typedef T Tail;
};
struct null_typelist {};
template<int... Indices>
struct indices {
using next = indices<Indices..., sizeof...(Indices)>;
};
template<int Size>
struct build_indices {
using type = typename build_indices<Size - 1>::type::next;
};
template<>
struct build_indices<0> {
using type = indices<>;
};
template<typename T>
using Bare = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
template<typename Tuple>
constexpr
typename build_indices<std::tuple_size<Bare<Tuple>>::value>::type
make_indices()
{ return {}; }
template<typename T, typename... Args>
struct tuple_push;
template<typename T, typename... Args>
struct tuple_push<T, std::tuple<Args...>>
{
typedef std::tuple<Args..., T> type;
};
template<typename TL>
struct typelist_to_tuple;
template<typename H, typename T>
struct typelist_to_tuple<typelist<H, T>>
{
typedef typename tuple_push<H, typename typelist_to_tuple<T>::type>::type type;
};
template<typename H>
struct typelist_to_tuple<typelist<H, null_typelist>>
{
typedef std::tuple<H> type;
};
struct Float {
float get() const { return 7.5; }
};
struct Int {
int get() const { return 7; }
};
template<typename... Args>
void foo(const Args&... args)
{
}
template<typename T, typename... Args>
void foo(const T& current, const Args&... args)
{
std::cout << current << std::endl;
foo(args...);
}
template<typename Tuple, int... Indices>
void apply(const Tuple& tuple, indices<Indices...>)
{
foo(std::get<Indices>(tuple).get()...);
}
template<typename Tuple>
void apply(const Tuple& tuple)
{
apply(tuple, make_indices<Tuple>());
}
int main()
{
typedef typelist<Int, typelist<Float, typelist<Int, null_typelist>>> list;
typedef typelist_to_tuple<list>::type tuple;
tuple t = std::make_tuple(Int(), Float(), Int());
apply(t);
}