它很可能重新n階論點爲O(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