modern c++ design

本文是对《Modern C++ Design》一书的读后感,探讨了静态检查、无名域名、局部类、编译期分支选择等关键概念。通过实例解析了如何利用C++的特性进行高效设计,如编译期类型检查和继承确认。
摘要由CSDN通过智能技术生成

modern c++ design 读后有感

一、静态检查

1、通过编译器数组长度不能为0设计
// 编译器检查:编译期无法分配0元素数组

#define STATIC_CHECK(expr) { char unnamed[(expr) ? 1 : 0]; }

2、通过模板特性,不使用相关代码时,代码是不会实例化的

template<bool> struct CompileTimeError;
template<> struct CompileTimeError<true> {};
#define STATIC_CHECKT(expr) \
(CompileTimeError<(expr) != 0>())
表达式expr为false时,会提示没有实例化而报错

二、无名域名

无名域名使用时,只能在本文件使用,例如:
namespace
{
。。。。。。
}

三、Local class

Local classes are an interesting and little-known feature of C++. You can define classes right inside functions, as follows:

void Fun()
{
class Local
{
... member variables ...
... member function definitions ...
};
... code using Local ...
}

There are some limitations—local classes cannot define static member variables and cannot access nonstatic local variables. What makes local classes truly interesting is that you can use them in template functions. Local classes defined inside template functions can use the template parameters of the enclosing function

class Interface
{
public:
    virtual void Fun() = 0;
};
template <class T, class P>
Interface* MakeAdapter(const T& obj, const P& arg)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值