量化交易之C++篇 - 可变参数模板(模板遍历元组、模板作模板参数 & using定义模板别名)

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值