#include <iostream>
#include <memory>
#include <string>
#include <map>
#include <utility>
#include <vector>
#include <list>
#include <tuple>
#include <thread>
using namespace std;
namespace tupletest { // 用模板遍历元组
template<int count, int varCounts, typename ...Others>
class MyClass{
public:
static void function(const tuple<Others...>& t) {
std::cout << "value: " << get<count>(t) << std::endl;
MyClass<count + 1, varCounts, Others...>::function(t);
}
};
template<int varCounts, typename ...Others>
class MyClass<varCounts, varCounts, Others...> {
public:
static void function(const tuple<Others...>& t) {
}
};
template<typename ...Others>
void function(const tuple<Others...>& t) {
MyClass<0, sizeof...(Others), Others...>::function(t);
}
void test() {
tuple<int, double, string> foo {10, 1.5, "tqz"};
// std::cout << get<0>(foo) << std::endl;
// std::cout << get<1>(foo) << std::endl;
// std::cout << get<2>(foo) << std::endl;
function(foo);
}
}
namespace templatetest { //模板作为模板参数, using 定义模板别名
template<typename T, template<typename W> class Container>
class MyClass {
public:
MyClass() {
for (int index(0); index < 10; index++) {
this->container.push_back(index);
}
};
private:
T value;
Container<T> container;
};
template<typename T> using myList = list<T, allocator<T>>;
template<typename T> using myVector = vector<T, allocator<T>>;
void test() {
MyClass<int, myList> mylist;
MyClass<int, myVector> myvector;
}
}
int main() {
// tupletest::test();
templatetest::test();
return 0;
}
量化交易之C++篇 - 可变参数模板(模板遍历元组、模板作模板参数 & using定义模板别名)
最新推荐文章于 2023-01-29 19:03:32 发布