
c++ Idioms
DONOT_WORRY_BE_HAPPY
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Idiom : pimpl
用c++ 11实现:#include <memory>class MyClass {public: MyClass(); ~MyClass(); void DoSomeThing();private: class MyClassImpl; std::unique_ptr<MyClassImpl> impl_;};#include "My...原创 2018-06-23 21:14:17 · 404 阅读 · 0 评论 -
Idiom: Non-copyable
用c++98:class NoCopyable {public: NoCopyable() {} ~NoCopyable() {}private: NoCopyable(const NoCopyable&); // Not implemented. NoCopyable& operator=(const NoCopyable&); // Not imp...原创 2018-06-23 21:20:14 · 219 阅读 · 0 评论 -
Idiom: Copy-on-write
最简单的cow:class SimplestCOW {public: void Write(int value) { if (!data_ || !data_.unique()) data_ = std::make_shared<int>(); *data_ = value; }private: std::shared_ptr<int...原创 2018-06-23 21:40:04 · 185 阅读 · 0 评论 -
Idiom:Scope Guard
最简单的Scope Guardclass SimplestScopeGuard {public: SimplestScopeGuard(std::function<void()> func) : func_(func), dismiss_(false) {} ~SimplestScopeGuard() { if (func_ && !dismiss_)...原创 2018-06-23 21:53:34 · 203 阅读 · 0 评论 -
Idiom:Copy-and-swap
简单的实现:class Myclass {public: Myclass(size_t size) : data_(size ? new(std::nothrow) int[size] : nullptr) , size_(!data_ ? 0 : size){} Myclass(const Myclass& other) { data_ = other.size_ ? ...原创 2018-06-24 11:08:52 · 212 阅读 · 0 评论 -
Idiom:Include Guard Macro
1. #pragma once编译器支持情况:Compiler#pragma onceARM DS-5Supported[18]C++Builder XE3Supported[10]ClangSupported[7]Comeau C/C++Supported[8]Cray C and C++Unsupported as of version 8.6[9]Digital Mars C++Suppor...原创 2018-06-24 11:21:27 · 264 阅读 · 0 评论 -
Idiom:Multi-statement Macro
用do{}while(false)封装避免分号带来的问题。#define IntValueInit(P1, P2) \ do{ \ P1 = 1; \ P2 = 2; \ }while(false)原创 2018-06-24 11:31:28 · 256 阅读 · 0 评论 -
Idiom:Shrink-to-fit
一些STL容器如std::string,std::vector会申请比实际写入的字节更多的内存还优化效率,这个Idiom可以让它们的内存没有多余的。std::vector<int> v;std::vector<int>(v.begin(), v.end()).swap(v);...原创 2018-06-24 11:45:46 · 302 阅读 · 0 评论