
c++模版编程
蓝鲸123
做更好的自己
展开
-
c++模版类特化
1. 提供一种能自动识别类和类构造函数参数类型,并进行warp的函数类,重新修改类内成员变量#include <iostream>#include <thread>// 提供一种能自动识别类和类构造函数参数类型,并进行warp的函数类,重新修改类内成员变量// Signature : T(Args...)// ->// void(*)(T*, Args...)template<class T>struct FuncAWrapper;templat原创 2020-12-27 00:30:36 · 268 阅读 · 0 评论 -
C++模板进阶指南:SFINAE
来自于 Substitution failure is not an error 的首字母缩写。Substitution,Failure和Error三个词构成。详细的参考:cpprefernceSFINAE保证了在编译时期对类型进行推断,如果你拥有一个模版函数。你有两种需求,第一种是模版函数只支持特定类型的参数。第二种需求是对于重复定义函数发生的编译错误,通过SFINE放在函数的参数列表中,以此避免参数重复定义报错。。第一种是模版函数只支持特定类型的参数#include <iostream原创 2020-12-05 17:38:27 · 435 阅读 · 0 评论 -
在JIT编译时期进行类型检查
#include <iostream>#include <list>#include <vector>using std::list;#include <map>template <typename T>using vector = std::vector<T>;#define DEF_IS(check_type, return_type) \ template<class T> \ typ原创 2020-05-24 13:24:10 · 305 阅读 · 0 评论 -
c++ 实现对象迭代器
#include <iostream>#include <list>#include <vector>using std::list;template <typename T, typename To>struct Caster { list<To> *ptr; Caster(list<To>* ptr) : ptr(ptr) {}; struct Iter { typename l原创 2020-05-23 16:13:40 · 535 阅读 · 0 评论 -
参数值递归模版类
#include <iostream>#include <map>namespace teaflow {template<std::size_t...> struct seq{ void pint() { std::cout << "origin seq" << std::endl; }};template&...原创 2020-04-08 00:25:40 · 167 阅读 · 0 评论 -
c++不同数据处理类型函数,使用宏统一命名
#include <iostream>#include <map>namespace teaflow {#define function_alias(A, B) \template <typename... Arg> \auto B(Arg&&... arg) -> decltype(A(std::forward<Arg&...原创 2020-04-07 23:35:16 · 498 阅读 · 0 评论 -
多种模版类函数:使用模版方法,重载operator() 构造模版函数类
#pragma once#include <functional>#include <type_traits>#include <memory>#include <iostream>namespace internal{ template <class R, class... Args> class Func...原创 2019-07-11 00:04:21 · 740 阅读 · 0 评论 -
c++11新特性std::is_trivial
首先 std::is_trivila 定义:template< class T >struct is_trivial;结构成员函数: value返回true,如果T 包含默认的构造函数。其他情况下,返回false。一种可能的实现方式:template< class T >struct is_trivial : std::integral_constant&...原创 2019-07-10 21:49:56 · 5537 阅读 · 0 评论 -
std::enable_if 和enable_if_t 搭配 is_same编译时期类型检查
class AT{public: void PrintT(){ std::cout << "PrintT" << std::endl; } int ati;};int main() { std::enable_if_t<std::is_class<AT>::value,AT> ati; /...原创 2019-07-10 12:27:52 · 5670 阅读 · 1 评论 -
c++使用宏检测类是否包含某个函数或者变量属性
对于特定的函数或者变量属性检测一个特定的参数类:#include <iostream>#include <boost/smart_ptr.hpp>#include <type_traits>using namespace boost;using namespace std;#define _CAT(A, B) A##B#define STR(s...转载 2019-06-05 23:12:19 · 2194 阅读 · 1 评论 -
C++中的friend详细解析
C++中的友元机制允许类的非公有成员被一个类或者函数访问,友元按类型分为三种:普通非类成员函数作为友元,类的成员函数作为友元,类作为友元。友元包括友元的声明以及友元的定义。友元的声明默认为了extern,就是说友元类或者友元函数的作用域已经扩展到了包含该类定义的作用域,所以即便我们在类的内部定义友元函数也是没有关系的。友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类...转载 2019-01-19 10:23:07 · 4012 阅读 · 0 评论 -
c++模板编程,搜索二叉树构造
// 树的迭代器tree_iterator.h//// Created by Yongyu Wu on 2018/11/11.////树节点的迭代器#ifndef C11TEMPLATE_TREE_ITERATOR_H#define C11TEMPLATE_TREE_ITERATOR_H#pragma oncetemplate <typename N>class...原创 2018-11-11 17:10:39 · 288 阅读 · 0 评论 -
模版参数的声明与使用
#include &lt;iostream&gt;using namespace std;template &lt;typename T, unsigned size&gt;class array2{// static const unsigned size =10; T elems[size];public: T&amp;operator[](unsigned...原创 2018-11-05 21:27:21 · 677 阅读 · 0 评论 -
c++ 模版编程,解析输入命令argv,argc
下面的代码是从 CUB中摘录出来的。/** * Utility for parsing command line arguments */struct CommandLineArgs{ std::vector<std::string> keys; std::vector<std::string> values; std::ve...原创 2018-11-07 15:47:07 · 1682 阅读 · 0 评论 -
c++ 模版编程,operator重载
#pragma once#include &amp;lt;iostream&amp;gt;#include &amp;lt;stdexcept&amp;gt;class half_t{ uint16_t __x;public: half_t():__x(0){} half_t(int a){ *this=half_t(float(a)); } half_t(con...原创 2018-11-07 15:14:34 · 420 阅读 · 0 评论 -
c++ 模版编程,构造迭代器和双向链表
#pragma once#include <iostream>#include <stdexcept>#include "func.h"// 链表 template <typename T>class list; //前置声明,用于声明友类 template <typename N> class list_iterat...原创 2018-11-07 12:39:51 · 492 阅读 · 0 评论 -
c++模版编程构造栈和向量vector
c++模版编程构造栈和向量vector向量栈向量 //使用 声明通例template <typename T>class my_vector{ T* my_array; unsigned size; unsigned block_size;public: my_vector(unsigned bsz):my_array((T*)ma...原创 2018-11-06 17:02:13 · 421 阅读 · 0 评论