实例代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <list>
#include <unordered_set>
#include <functional>
#include <queue>
using namespace std;
namespace _nmsp1
{
template <typename... T>
void myfunct1(T... args)
{
cout << sizeof...(args) << endl;
cout << sizeof...(T) << endl;
}
void myfunct2()
{
cout << "参数包展开时执行了递归终止函数myfunc2()..." << endl;
}
template <typename T, typename...U>
void myfunct2(const T& firstarg, const U&... otherargs)
{
cout << "收到的参数值为:" << firstarg << endl;
myfunct2(otherargs...);
}
void func()
{
myfunct2(10, "abc", 12.7);
}
}
namespace _nmsp2
{
template<typename...Args> class myclasst {};
template<> class myclasst<>
{
public:
myclasst()
{
printf("myclasst<>::myclasst()执行了,this = %p\n", this);
}
};
template <typename First, typename... Others>
class myclasst<First, Others...> : private myclasst<Others...>
{
public:
myclasst() :m_i(0)
{
printf("myclasst::myclasst()执行了,this = %p\n", this);
}
myclasst(First parf, Others...paro) :m_i(parf), myclasst<Others...>(paro...)
{
cout << "-------------------begin----------------" << endl;
printf("myclasst::myclasst(parf,paro)执行了,this = %p\n", this);
cout << "m_i=" << m_i << endl;
cout << "-------------------end------------------" << endl;
}
First m_i;
};
void func()
{
myclasst<int, float, double> myc(12, 13.5f, 23);
}
}
int main()
{
_nmsp2::func();
return 1;
}