#include "def.h"
// def.h 源码
// https://blog.csdn.net/qq_34179431/article/details/120977389
// ************************ transform
template<typename List, template<class> typename MetaFunc, bool = IsEmpty_v<List>>
struct Transform_;
// 通用型
template<typename List, template<class> typename MetaFunc>
struct Transform_<List, MetaFunc, false> {
private:
using Front = Front_t<List>;
using Tail = typename Transform_<PopFront_t<List>, MetaFunc>::type;
public:
using type = PushFront_t<Tail, typename MetaFunc<Front>::type>;
};
template<typename List, template<class> typename MetaFunc>
struct Transform_<List, MetaFunc, true> {
using type = List;
};
// 特化版
template<template<class ...name> typename List, template<class> typename MetaFunc, typename ...Types>
struct Transform_<List<Types...>, MetaFunc, false> {
using type = List<typename MetaFunc<Types>::type...>;
};
template<template<class ...name> typename List, template<class> typename MetaFunc>
struct Transform_<List<>, MetaFunc, true> {
using type = List<>;
};
template<typename List, template<class> typename MetaFunc>
using Transform_t = typename Transform_<List, MetaFunc>::type;
// 实践一
// 给每个类型增加 const
template<typename List>
struct AddConstToAll_ {
using type = Transform_<List, add_const>;
};
template<typename List>
using AddConstToAll_t = typename Transform_<List, add_const>::type;
int main() {
using SL = TypeList<int, char, long>;
using SSL = AddConstToAll_t<SL>;
cout << boost::typeindex::type_id_with_cvr<SSL>().pretty_name() << endl;
}
c++-模板元编程TypeList进阶操作 transform
最新推荐文章于 2023-11-03 13:26:42 发布