Here's a working example:
#include <vector>
// indices machinery
template< std::size_t... Ns >
struct indices {
typedef indices< Ns..., sizeof...( Ns ) > next;
};
template< std::size_t N >
struct make_indices {
typedef typename make_indices< N - 1 >::type::next type;
};
template<>
struct make_indices< 0 > {
typedef indices<> type;
};
void f(int) {}
void f(int, int) {}
// helper function because we need a way
// to deduce indices pack
template<size_t... Is>
void call_helper(const std::vector<int>& args, indices<Is...>)
{
f( args[Is]... ); // expand the indices pack
}
template<std::size_t Arity>
void call(const std::vector<int>& args)
{
if (args.size() < Arity) throw 42;
call_helper(args, typename make_indices<Arity>::type());
}
int main()
{
std::vector<int> v(2);
call<2>(v);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…