cxx CRTP Curiously Recurring Template Pattern 的概念 C++ 通过类的继承与虚函数的动态绑定,实现了多态。这种特性,使得我们能够用基类的指针,访问子类的实例。例如我们可以实现一个名为 Animal 的基类,以及 Cat, Dog 等子类,并通过在子类中重… ...更多
cxx std::enable_shared_from_this c++ std::enable_shared_from_this 在很多应用中,我们可能希望对象永远都存在,特别是在一些异步执行的程序中,回调函数的传入是需要保证对象指针的有效性的,所以,我们一般都是传入共享指针,我们就用到了std:… ...更多
decltype auto decltype decltype(auto) foo(auto t1, auto t2) { return t1+t2; }
int x = 1; int main() { int xx = 11; decltype(x) xr = x; … ...更多
spaceship operator operator<=> for C++20 bool operator<(const Name& other) const { //before c++11 return first_name<othe… ...更多
31 nooby cxx habits you need to ditch 31 nooby cxx habits you need to ditch
using namespace std using std endl in a loop index based for when range-f… ...更多
template auto Advantages of auto in template parameters in C++17 The template <auto> feature (P0127R1) was accepted into C++ in the ISO C++ 2016… ...更多
using 新用法 引入命名空间 可能在C++的初学者眼中,using关键字就是对常见的是这两句代码: using namespace std;和using std::cout; 当然,这也是using的最基本的使用方法。 using关键字在类继承的中的作用 ##(1)派生类部分覆盖基类的成员函数 … ...更多
类的默认函数 在C++中一个类有八个默认函数 默认构造函数; 默认拷贝构造函数; 默认析构函数; 默认重载赋值运算符函数; 默认重载取址运算符函数; 默认重载取址运算符const函数; 默认移动构造函数(C++11); 默认重载移动赋值操作符函数(C++11)。 只是声明一个空类,不做任何事情的话,… ...更多
emplace_back emplace_back({1,2})方式 https://stackoverflow.com/questions/20391632/how-to-use-stdvectoremplace-back-for-vectorvectorint #include <init… ...更多
tuple sum 遍历std::tuple #include <tuple> #include <iostream>
using std::cout; using std::make_tuple;
template<typename T> std::enab… ...更多
coding variable 记录常用变量名 variable name ceil floor round trancate trim below above foo bar tmp begin end start stop final finish forward back front back… ...更多
PIMPL 模式 PIMPL:很实用的一种基础模式。 PIMPL解释 PIMPL(Private Implementation 或 Pointer to Implementation)是通过一个私有的成员指针,将指针所指向的类的内部实现数据进行隐藏。 PIMPL的优点: 1)降低模块的耦合。因为隐藏… ...更多
cxx11内存顺序 背景 x86_64和Sparc是强顺序模型(Total Store Order),这是一种接近程序顺序的顺序模型。所谓Total,就是说,内存(在写操作上)是有一个全局的顺序的(所有人看到的一样的顺序), 就好像在内存上的每个Store动作必须有一个排队,一个弄完才轮到另一个,这… ...更多
内存顺序问题有两个来源:编译器优化可能改变代码中读写指令的顺序;CPU 在运行时也可能重排指令(如 Tomasulo 算法)。单线程或使用 Mutex/Semaphore 时,编译器和 CPU 保证语义一致,开发者无需关心内存顺序;但当使用原子操作进行无锁编程时,内存模型的抽象被打破,开发者必须显式规约。 ...更多
内存顺序(Memory Order) 内存顺序 内存顺序,通俗地讲,是关于代码编译成机器指令后的执行顺序问题。内存顺序和编译器、硬件架构密切相关。那为什么会产生内存顺序问题呢?有两方面原因: 一方面,编译器为了优化程序性能,不会完全按照开发者写的代码的顺序来生成机器指令; 另一方面,在程序运行时,为… ...更多
shared_ptr 和make_shared 介绍 条款21:尽量使用std::make_unique和std::make_shared而不直接使用new std::make_unique 和 std::make_shared是三个make函数中的两个,make函数用来把一个任意参数的集合完美转移… ...更多
为什么不提倡使用vector 为什么不提倡使用vector<bool> vector<bool>并不是一个STL容器,不是一个STL容器,不是一个STL容器!首先vector<bool> 并不是一个通常意义上的vector容器,这个源自于历史遗留问题。 早在C++… ...更多
二叉树后序遍历 二叉树的后序遍历算法比较难,尤其是非递归。但是后序遍历的应用最广泛,最容易出现算法题,因此一定要牢牢掌握。其中迭代的方法又较递归的方法快,但是也是非常令人头疼的一件事。 1.递归算法 public void postOrderRecur(TreeNode root){ if(… ...更多
vscode using remote docker Docker开启远程端口 在远程主机上: 创建文件 daemon.json 到目录 /etc/docker: {"hosts": ["tcp://0.0.0.0:2375", &quo… ...更多
lock free vs wait free 在提到Concurrent Non-blocking算法的时候,都会遇到对无锁算法的无锁程度理解. 什么是wait-free和lock-free 所谓lock-free和wait-free算法是指对于共享的数据并非对其加锁来控制访问,而是多个线程并行的访… ...更多