《Effective Modern C++》条款3

条款3:理解decltype(Understand decltype)

decltype可以告诉你一个变量或一个表达式的类型。

一般情况下,decltype返回的类型和你所给变量或表达式的类型一模一样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const int i = 0;          // decltype(i) is const int
bool f(const Widget& w); // decltype(w) is const Widget&
// decltype(f) is bool(const Widget&)

struct Point {
int x, y;
}; // decltype(Point::x) is int
// decltype(Point::y) is int

Widget w; // decltype(w) is Widget

if(f(w)) ... // decltype(f(w)) is bool


template<typename T> // simplified version of std::vector
class vector {
public:
...
T& operator[](std::size_t index);
...
};

vector<int> v; // decltype(v) is vector<int>
...
if (v[0] == 0) ... // decltype(v[0]) is int&

《Effective Modern C++》条款2

条款2:理解auto类型推导(Understand auto type deduction)

除了在一种情况下,auto类型推导和模板类型推导是一样的。

条款1中,使用以下统一的函数模板:

1
2
template<typename T>
void f(ParamType param);

和统一调用方式:

1
f(expr);

在函数是f的调用过程中,编译器使用expr来推断TParamType

当一个变量使用auto来声明时,auto扮演了模板中T的角色,且该变量的类型说明符扮演了ParamType的角色。因此,同条款1中一样,根据变量的类型说明符的形式分三种情况:

  • 情况1:类型说明符是一个引用或指针,但不是一个万能引用(Universal Reference)
  • 情况2:类型说明符是一个万能引用(Universal Reference)
  • 情况3:类型说明符既不是指针也不是引用

《Effective Modern C++》条款1

条款1:理解模板类型推导(Understand template type deduction)

模板的类型推导是现代C++最引人注目的特性之一auto的基础,因此要真正理解模板类型推导的各个方面。

函数模板:

1
2
template<typename T>
void f(ParamType param);

调用方式:

1
f(expr);  // call f with some expression

在编译过程中,编译器使用expr来推导两个类型:TParamType。被推导出的T的类型不仅取决于expr的类型,还取决于ParamType的形式。有三种情形:

PEP 0008 -- Python代码风格指南

译自:https://www.python.org/dev/peps/pep-0008/

PEP: 8
Title: Style Guide for Python Code
Author: Guido van Rossum , Barry Warsaw , Nick Coghlan
Status: Active
Type: Process
Created: 05-Jul-2001
Post-History: 05-Jul-2001, 01-Aug-2013

介绍

本文对Python的主要发行版本的标准库代码给出了编码约定。关于PythonC实现中的C风格指南请见相关的PEP[1]

本文和PEP 257文档字符串约定)改编于Guido最初的Python风格指南文章,并从Barry的风格指南中汲取了一些内容[2]

因为语言本身的变化,本风格指南也随着时间的推移而演变,更多的约定被发现,过时的约定被淘汰。

许多的项目有他们自己的编码风格指南。如果和本指南发生了冲突,项目应该居先考虑项目特定的指南。

当DP遇见Py(二十) -- 职责链模式

定义:

使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。

类图:

类型:行为型

实例:

请假由谁批要看天数?