c 可變引數模板

2021-08-23 12:38:40 字數 1616 閱讀 7176

可變模板引數函式

1.逗號表示式展開引數包

templatevoid expand(const f& f, args... args);}

//expand((auto i), 1, 2.0, 」test」);

2.遞迴函式方式展開

template void printarg(t&& t)

//終止遞迴

templatevoid processvalues(t &&arg)

templatevoid processvalues(t &&arg, ts&& ...args)

可變模板引數類

1.模版偏特化和遞迴方式來展開引數包

//前向宣告

templatestruct sum;

//基本定義

templatestruct sum;

};//遞迴終止

templatestruct sum;

};

2.繼承方式展開引數包

//前項宣告

templateclass mytupe;

//私自繼承

templateclass mytupe:private mytupe

mytupe(head v, tail... vtail) :m_head(v), inherited(vtail...)

{} head head()

inherited& tail()

};

//遞迴終止

template<> class mytupe<>{};

3.組合方式展開引數包

templateclass mytup;

templateclass mytup

mytup(head v, tail... vtail) :m_head(v), m_tail(vtail...)

{} head head()

composited& tail()

};//遞迴終止

template<> class mytup<>{};

例項:

建立乙個委託

templateclass mydelegate

r operator()(args&&... args)

private:

t* m_t;

r(t::*m_f)(args...);

};templatemydelegatecreatedelegate(t* t, r(t::*f)(args...))

struct a

void fun1(int i, double j)

};//a a;

//auto d = createdelegate(&a, &a::fun); //建立委託

//d(1); //呼叫委託,將輸出1

//auto d1 = createdelegate(&a, &a::fun1); //建立委託

//d1(1, 2.5); //呼叫委託,將輸出3.5

C 可變引數模板

c 可變引數模板 flyfish c 98版本不支援 c 11版本以上支援 arguments 是引數包 parameter pack 類 classname 可以接受可變引數個數 template class custom tuple custom tuple c1 custom tuple c2...

C 可變引數模板

乙個可變引數模板 variadic template 就是乙個接受可變數目引數的函式模板或類模板。可變數目的引數被稱為引數包 parameter packet 存在兩種引數包 模板引數包 template parameter packet 表示0個或多個模板引數 函式引數包 function par...

可變引數模板

乙個可變引數模板就是乙個接受可變數目引數的模板函式或模板類。可變數目的引數被稱為引數包 parameter packet 存在兩種引數包 模板引數包 template parameter packet 表示零個或多個模板引數 函式引數包 function parameter packet 表示零個或...