C11-可变参数模板-Variadic Templates可变参数模板
程序员文章站
2022-04-05 08:43:28
...
/**************************************************************************************
************************* 1.Variadic Templates可变参数模板 *************************
***************************************************************************************/
namespace BaseFeatures_NameSpace
{
//此处一定要有一个无参的ShowInformation()定义或者声明在可变参数模板版的前面
//该函数结束编译期递归,在解包参数为0时调用
void ShowInformation()
{
cout << endl;
}
/*
用法:
hello();
template<typename T,typename... args>
hello(T InParam1,Types&... args)
{
hello(...args);//递归
}
*/
template<typename T, typename... Types>
void ShowInformation(const T& firstArg, const Types&... args)
{
cout << "编译期递归中" << endl;
cout << firstArg << endl;
ShowInformation(args...);
}
template<typename... Types>
void ShowInformation(const Types&... args)
{
cout << "编译期递归中,我比较没存在感,所以有楼上就不调我" << endl;
ShowInformation(args...);
}
template<typename T>
void ShowInformation(const T& firstArg)
{
cout << "偶是非递归的额外测试,测试能不能拦截下来递归,但是失败了" << firstArg << endl;
}
/**************************************************************************************
************************************ 1.1实例 ************************************
***************************************************************************************/
//利用重载解决问题
//数据解析成对象
class VarTemplateExample_Data
{
int m_iID;
int m_iData;
};
class VarTemplateExample
{
public:
const char* m_strInstanceName;
int m_InstanceID;
//vector<float> m_dataContainer;
vector<VarTemplateExample_Data> m_dataContainer;
template< typename... Types>
void ResolveInformation(const char* In_strInstanceName,int In_ID, Types&... args)
{
cout << "ResolveInformation (const char* In_strInstanceName, T a, const Types&... args)" << endl;
m_strInstanceName = In_strInstanceName;
m_InstanceID = In_ID;
ResolveInformation(args...);
}
template<typename... Types>
void ResolveInformation(int In_ID, Types&... args)
{
const char* name = "DefaultName";
cout << "ResolveInformation(int In_ID,const Types&... args)" << endl;
ResolveInformation(name, In_ID, args...);
}
template<typename... Types>
void ResolveInformation(VarTemplateExample_Data& Obj, Types&... args)
{
cout << "ResolveInformationVarTemplateExample_Data Obj, const Types&... args" << endl;
m_dataContainer.push_back(Obj);
ResolveInformation( args...);
}
//只有通过特别指定的方式才能被调用
template<typename T>
void ResolveInformation(VarTemplateExample_Data& firstArg)
{
cout << "ResolveInformation(const T& firstArg)" << endl;
cout << "偶是非递归的额外测试,测试能不能拦截下来递归,但是失败了" << endl;
}
void ResolveInformation()
{
cout << endl;
}
private:
};
}
{
BaseFeatures_NameSpace::VarTemplateExample temp;
BaseFeatures_NameSpace::VarTemplateExample temp1;
BaseFeatures_NameSpace::VarTemplateExample temp2;
BaseFeatures_NameSpace::VarTemplateExample temp3;
BaseFeatures_NameSpace::VarTemplateExample_Data obj1;
BaseFeatures_NameSpace::VarTemplateExample_Data obj2;
BaseFeatures_NameSpace::VarTemplateExample_Data obj3;
BaseFeatures_NameSpace::VarTemplateExample_Data obj4;
int i = 0;
const char* str = "Test1";
SHOW_SMALL_FUNCTION_BLOCK_LIST_TIPS("不同参数个数的")
temp.ResolveInformation(str,1,obj1,obj2);
std::cout << endl; std::cout << endl;
temp1.ResolveInformation( 1, obj1, obj2);
std::cout << endl; std::cout << endl;
temp2.ResolveInformation( obj1, obj2);
std::cout << endl; std::cout << endl;
//只有通过这种方式才会被调用
temp3.ResolveInformation<BaseFeatures_NameSpace::VarTemplateExample_Data&>(obj1);
std::cout << endl; std::cout << endl;
}
上一篇: 使用fiddler抓包的实例教程