编写这个东西的起因是一些关于静态反射性c++的思考,不过本质上用处不大,除非你全部的类都用tuple实现。有兴趣的可以当做一种对于c++奇技淫巧的学习,没兴趣也不用看。
main.c
#include <iostream>
#include <list>
#include <vector>
#include <numeric>
#include "types_vector.h"
#include "types_vector_merger.h"
using namespace template_type_traits;
template<typename Arg>
struct Check
{
enum
{
value = std::is_same<int, Arg>::value || std::is_same<char, Arg>::value
};
};
int main()
{
//types_vector的功能
//apply 将所有内部类型转接
//arg 读取
//remove_if 条件移除,前后联合性的话可以使用remove_if_by_iterator,不过对于内部实现要有一定了解
//merge 合并其他容器
//erase下标移除
//insert 插入
//change 改变
//explos 拆除某类型,一般来说不应该提供这个功能
using typeArgA = types_vector<std::vector<int>, bool, char>;
typeArgA::remove_if<Check>::change<2, int> a;
std::cout << typeid (decltype (a) ::arg<2>).name() << "\n";
return 0;
}
types_vector.h
#ifndef TYPES_HANDLER_H
#define TYPES_HANDLER_H
#include "template_types_base_class.h"
#include "types_condition_remove_checker.h"
#include "types_vector_merger.h"
namespace template_type_traits
{
//条件转接器
template<template<typename Arg> class CheckClass>
struct type_check_to_iterator_check
{
template<typename Iterator>
struct check
{
enum
{
value = CheckClass<typename Iterator::arg>::value
};
};
};
template <template<typename Arg> class CheckClass>
struct type_check_operator_not
{
template<typename Iterator>
struct check
{
enum
{
value = !CheckClass<Iterator>::value
};
};
};
//索引效验器
template<size_t from, size_t to>
struct index_check
{
template<typename Iterator>
struct check
{
enum
{
value = Iterator::effect && Iterator::index >= from && Iterator::index < to
};
};
};
//可变类型参数处理器
template<typename ...Args>
struct types_vector
{
enum
{
ARGS_NUM = sizeof... (Args)
};
template < size_t i, bool isEffect>
struct IteratorBase;
template < size_t i, bool isEffect =