//学习地址: http://c.biancheng.net/view/3730.html
template <typename T, typename U = int, U N = 0>
struct TestTemplateStruct {
};
template <typename T = int>
void TestTemplateFunc() {
T a;
cout << "a = " << a << endl;
}
template <typename R = int, typename U>
R TestTemplateWithCompiler(U val)
{
return val;
}
void TestTemplateWithCompiler1() {
TestTemplateWithCompiler(0);
TestTemplateWithCompiler<int , char>(0);
TestTemplateWithCompiler<int>(1);
}
void mutableParamsFunc(int count, ...) {
va_list params;
va_start(params, count);
for (int i = 0; i < count; ++i) {
int param = va_arg(params, int);
cout << "param = " << param << endl;
}
va_end(params);
}
param = 1
param = 2
param = 3
param = -88828182
void mutableParamsFunc1(int count, ...) {
va_list params;
va_start(params, count);
for (int i = 0; i < count; ++i) {
int int_param = va_arg(params, int);
char* char_param = va_arg(params, char*);
cout << "int_param = " << int_param << endl;
cout << "char_param = " << char_param << endl;
}
va_end(params);
}
//递归出口
void mutableParamsFunc2() {
cout << " mutableParamsFunc2 end" << endl;
}
template<typename T, typename... args>
void mutableParamsFunc2(T argc, args... argv) {
cout << argc << endl;
mutableParamsFunc2(argv...);
}
5.第二种方式,非递归
template<typename T>
void handleMutableParamsFunc3(T t) {
cout << t << endl;
}
template<typename... args>
void mutableParamsFunc3(args... argv) {
int arr[] = {(handleMutableParamsFunc3(argv) , 0)...};
}
#include
using std::tuple;
1) 默认构造函数
constexpr tuple();
2) 拷贝构造函数
tuple (const tuple& tpl);
3) 移动构造函数
tuple (tuple&& tpl);
4) 隐式类型转换构造函数
template <class... UTypes>
tuple (const tuple<UTypes...>& tpl); //左值方式
template <class... UTypes>
tuple (tuple<UTypes...>&& tpl); //右值方式
5) 支持初始化列表的构造函数
explicit tuple (const Types&... elems); //左值方式
template <class... UTypes>
explicit tuple (UTypes&&... elems); //右值方式
6) 将pair对象转换为tuple对象
template <class U1, class U2>
tuple (const pair<U1,U2>& pr); //左值方式
template <class U1, class U2>
tuple (pair<U1,U2>&& pr); //右值方式
void tupleTest() {
std::tuple<int, char> first(1, '1');
std::tuple<int, char> second(make_tuple(2, '2'));
auto third = make_tuple(3,'3'); //很舒服,不需要指明类型
}
void tupleTest1() {
std::tuple<int, char> first(1, '1');
std::tuple<int, char> second(make_tuple(2, '2'));
auto third = make_tuple(3,'3');
auto four = make_tuple("444", 4);
//1. swap 注意要是一个类型
//first.swap(four); //报错Non-const lvalue reference to type 'tuple' cannot bind to a value of unrelated type 'tuple'
first.swap(second);
//2. 返回对象 get()
cout << "get first 2 objecy = " << get<1>(first) << endl;
//3. 返回size tuple_size::value;
cout<< "return first tuple size = " << std::tuple_size<decltype(first)>::value << endl;
//4. 返回tie(args...) = 功能是将 tup 内存储的元素逐一赋值给 args... 指定的左值变量。
int num;
std::string str;
std::tie(num,str) = second;
cout<< "return second 1 = " << num << " , 2 = " << str << endl;
//5. 将多个 tuple 对象整合成一个tuple
auto five = tuple_cat(first, second);
}