【发布时间】:2017-02-02 12:19:33
【问题描述】:
假设我有一个只接受类型模板参数的函数,我无法更改它的定义/实现。
template < typename T >
void do_it();
现在我有一个以通常方式定义的类型列表,也无法更改:
template< typename ...Ts >
struct typelist;
我想实现一个函数,它接受一个类型列表,并在每个类型上运行 do_it():
template< typename List >
void do_them();
到目前为止,我发现的唯一解决方案是:
template< typename T >
void do_them_impl()
{
do_it<T>();
}
template< typename T, typename Ts...>
void do_them_impl()
{
do_it<T>();
do_them_impl<Ts...>();
}
template< template < typename...> class List, typename ...Ts >
void do_them_extract( List<Ts...>&& )
{
do_them_impl< Ts >();
}
template< typename List >
void do_them()
{
do_them_impl( List{} );
}
但这需要 4(!) 个函数,我想创建一个 do_them 函数。我将需要其中的很多,而且我不想为每个函数编写一个四边形。我错过了什么吗?
欢迎使用 C++14,也欢迎 C++17 解决方案,但标记为这样。
【问题讨论】:
标签: c++ templates c++14 variadic-templates c++17