사실 조금 전에 비슷한 코드를 작성했습니다. 그래서 다음과 같은 코드를 시도 :
template<unsigned N, unsigned M>
struct call_up_impl{
template<class Func, class Mutator, class Tuple, class... Args>
static void do_call(const Func& func, const Mutator& mutator, const Tuple& args, Args&&... unpacked_args) {
call_up_impl<N-1, M>::do_call(func, mutator, args, std::get<N-1>(args), std::forward<Args>(unpacked_args)...);
}
};
template<unsigned M>
struct call_up_impl<0, M> {
template<class Func, class Mutator, class Tuple, class... Args>
static void do_call(const Func& func, const Mutator&, const Tuple&, Args&&... unpacked_args) {
func(std::forward<Args>(unpacked_args)...);
}
};
template<unsigned M>
struct call_up_impl<M, M> {
template<class Func, class Mutator, class Tuple, class... Args>
static void do_call(const Func& func, const Mutator& mutator, const Tuple& args, Args&&... unpacked_args) {
call_up_impl<M-1, M>::do_call(func, mutator, args, mutator(std::get<M-1>(args)), std::forward<Args>(unpacked_args)...);
}
};
template<int i, typename Function, typename... Parms>
void apply(Function f, Parms... parms) {
std::tuple<Parms...> t(parms...);
call_up_impl<std::tuple_size<decltype(t)>::value, i + 1>::do_call(f, &g, t);
}
이 내 원래 코드의 빠른 적응이다, 그래서이 작업을 수행 할 수없는 최적의 방법을 철저하게 테스트 및 아마되지 않지만 적어도 (적어도 작동합니다 빠른 테스트에 따라 정확히 원하는대로). 튜플없이이 작업을 수행 할 수 있어야하지만 g ++로 컴파일 할 필요가 없습니다 (중첩 된 가변 템플릿이 필요하지는 않은 것 같습니다). 아마 튜플에 의해 도입 된 오버 헤드의 대부분을 피할 수
template<int i, typename Function, typename... Parms>
void apply(Function f, Parms&&... parms) {
std::tuple<Parms&&...> t(std::forward<Parms>(parms)...);
call_up_impl<std::tuple_size<decltype(t)>::value, i + 1>::do_call(f, &g, t);
}
그러나에 apply
을 변경. std::get
호출의 결과를 올바르게 전달하는 것이 더 좋을 지 모르지만, 나는 지금 그 일을 처리하는 데 너무 지쳤습니다.
pi는 math.h에서 M_PI로 사용할 수 있습니다. 나머지 부분에 대해서는 잘 모르겠지만 줄임표가 적은 코드를 게시 할 수 있습니까? –
@HansPassant 나는 OP가'pi '에 의해'ith-parameter'를 의미한다고 생각한다. –
@HansPassant C++가 이제는 모든 방향으로 나아 갔다 : 더 이상 모든 코드를 작성할 필요가 없다! 줄임표는 실제로 코드이며 실행 중입니다! –