それはO(n)のラッパーで再n次の引数にはかなり可能です:
#include <iostream>
using namespace std;
struct A { int a; A(int a) : a(a) {} };
struct B { int b; B(int b) : b(b) {} };
struct C { int c; C(int c) : c(c) {} };
struct D { int d; D(int d) : d(d) {} };
static void foo(A a, B b, C c, D d) { cout << a.a << " " << b.b << " " << c.c << " " << d.d << endl; }
template<class ...Args> struct Foo { void operator()(Args...); };
template<class ...Args> static void foo(Args ...args) { Foo<Args...>()(args...); }
template<class T, class U> struct Foo<T, U, C, D> { void operator()(T t, U u, C c, D d) { foo(u, t, c, d); } };
template<class T, class U, class V> struct Foo<T, U, V, D> { void operator()(T t, U u, V v, D d) { foo(v, t, u, d); } };
template<class T, class U, class V, class W> struct Foo<T, U, V, W> { void operator()(T t, U u, V v, W w) { foo(w, t, u, v); } };
int main() {
foo(A(1), B(2), C(3), D(4));
foo(D(5), C(6), B(7), A(8));
return 0;
}
(。の機能が部分的に専門的なことができないので、ラッパークラスFoo
が必要です)
$ c++ -std=c++11 a.cc
$ ./a.out
1 2 3 4
8 7 6 5
ドゥこれをこの手法の裏付けと解釈してはいけません。むしろ、可能であれば、これをしないでください。
ちょうど好奇心から、なぜ誰かが間違った順序で関数にパラメータを渡したいのですか? – DyZ
なぜこの柔軟性が必要ですか?それを構築する場合、定義された順序でそれらを渡すことができます。 –
関数が3つの異なる型の引数を取ることを知っているなら、関数prototyoeを調べるのではなく、順番どおりにそれらを渡すのが便利ではないでしょうか? – Arash