Cynosure对《API Design for C++》的笔记(5)

Cynosure
Cynosure (我是一只橘)

读过 API Design for C++

API Design for C++
  • 书名: API Design for C++
  • 作者: Martin Reddy
  • 页数: 472
  • 出版社: Morgan Kaufmann
  • 出版年: 2011-2-18
  • 第106页
    2011-10-20 10:37:13 2人喜欢 回应
  • 第148页

    CH4.7 Function Design 4.7.4 Error Handling P148

    By comparison, exceptions let your clients separate their error handling code from the normal flow of control, making for more readable code. They offer the benefit of being able to catch one or more errors in a sequence of several function calls, without having to check every single return code, and they let you handle an error higher up in the call stack instead of at the exact point of failure. An exception can also carry more information than a simple error code.
    引自第148页

    P149

    Typically the use of exceptions is an all-or-nothing proposition, meaning that if any part of an application uses exceptions then the entire application must be prepared to handle exceptions correctly. This means that the use of exceptions in your API also requires your clients to write exception safe code. It’s noteworthy that Google forbids the use of exceptions in their C++ coding conventions because most of their existing code is not tolerant of exceptions.
    引自第148页
    2012-02-02 14:09:06 回应
  • 第187页

    CH 6.4.3 Explicit Instantiation API Design If you want to provide only a predetermined set of template specializations for your API and disallow your users from creating further ones, then you do in fact have the option of completely hiding your private code. In this case, you can put your template definitions into a .cpp file and use explicit template instantiation to instantiate those specializations that you wish to export as part of your API. // stack.h #ifndef STACK H #define STACK H #include <vector> template <typename T> class Stack { public: void Push(T val); T Pop(); bool IsEmpty() const; private: std::vector<T> mStack; }; #endif // stack.cpp #include "stack.h" #include <string> template <typename T> void Stack<T>::Push(T val) { mStack.push back(val); } template <typename T> T Stack<T>::Pop() { if (IsEmpty()) return T(); T val mStack.back(); mStack.pop back(); return val; } template <typename T> bool Stack<T>::IsEmpty() const { return mStack.empty(); } // explicit template instantiations template class Stack<int>; template class Stack<double>; template class Stack<std::string>; The important lines here are the last three, which create explicit instantiations of the Stack class template for the types int, double, and std::string. The user will not be able to create further specializations (and the compiler will not be able to create implicit instantiations for the user either) because the implementation details are hidden in our .cpp file. However, our implementation details are now hidden successfully in our .cpp file. To indicate to your users which template specializations they can use (i.e., which ones you have explicitly instantiated for them), you could add a few typedefs to the end of your public header, such as typedef Stack<int> IntStack; typedef Stack<double> DoubleStack; typedef Stack<std::string> StringStack; It’s worth noting that by adopting this template style, not only do you (and your clients) get faster builds due to the removal of the overhead of implicit instantiation, but also, by removing the template definitions from your header, you reduce the #include coupling of your API and reduce the amount of extra code that your clients’ programs must compile every time they #include your API headers.

    2012-02-03 14:10:01 回应
  • 第193页

    6.5.3 Adding Operators to a Class class Currency { public: explicit Currency(unsigned int value); Currency::~Currency(); Currency(const Currency &obj); Currency &operator (const Currency &rhs); Currency &operator þ (const Currency &rhs); Currency &operator (const Currency &rhs); Currency &operator * (const Currency &rhs); Currency &operator / (const Currency &rhs); unsigned in GetReal() const; private: class Impl; Impl *mImpl; }; Currency operator þ(const Currency &lhs, const Currency &rhs); Currency operator (const Currency &lhs, const Currency &rhs); Currency operator *(const Currency &lhs, const Currency &rhs); Currency operator /(const Currency &lhs, const Currency &rhs); bool operator (const Currency &lhs, const Currency &rhs); bool operator ! (const Currency &lhs, const Currency &rhs); bool operator <(const Currency &lhs, const Currency &rhs); bool operator >(const Currency &lhs, const Currency &rhs); bool operator < (const Currency &lhs, const Currency &rhs); bool operator > (const Currency &lhs, const Currency &rhs); std::ostream& operator <<(std::ostream &os, const Currency &obj); std::istream& operator >>(std::istream &is, Currency &obj); 6.5.5 Conversion Operators A classic example is to define a custom string class that can be passed to functions that accept a const char * pointer, class MyString { public: MyString(const char *string); // convert MyString to a C style string operator const char *() { return mBuffer; } private: char *mBuffer; int mLength; }; // MyString objects get automatically converted to const char * MyString mystr("Haggis"); int same strcmp(mystr, "Edible"); int len strlen(mystr);

    2012-02-03 17:56:25 回应
  • 第223页

    Use explicit size-based types. The size of various types can differ by platform, compiler, and whether you are building a 32-bit or a 64-bit application. If you want to specify the exact size of a member variable, then you should use a type that specifically enforces this rather than assuming that types such as bool, short, or int will be a specific size. Unfortunately, the way to declare a fixed-size variable varies for different platforms. For example, on UNIX-based systems, the stdint.h header file provides types such as int8 t, uint32 t, and int64 t to specify an 8-bit integer, 32-bit unsigned integer, and a 64-bit integer, respectively. However, the Boost library provides platform-independent versions of these types in the boost/cstdint.hpp header. TIP Use size specific types, such as int32 t or uint16 t, to specify the maximum number of required bits for a variable. ------------------------------------------------------ 7.8.2 Random Access 1. The [] operator. This is meant to simulate the array indexing syntax of C/C++. Normally this operator is implemented without any bounds checking so that it can be made very efficient. 2. The at() method. This method is required to check if the supplied index is out of range and throw an exception in this case. As a result, this approach can be slower than the [] operator. ------------------------------------------------------ P253 8.4.4 Binary Compatibility Binary-Incompatible API Changes: * Removing a class, method, or function. * Adding, removing, or reordering member variables for a class. * Adding or removing base classes from a class. * Changing the type of any member variable. * Changing the signature of an existing method in any way. * Adding, removing, or reordering template arguments. * Changing a non-inlined method to be inlined. * Changing a non-virtual method to be virtual, and vice versa. * Changing the order of virtual methods. * Adding a virtual method to a class with no existing virtual methods. * Adding new virtual methods (some compilers may preserve binary compatibility if you only add new virtual methods after existing ones). * Overriding an existing virtual method (this may be possible in some cases, but is best avoided). Binary-Compatible API Changes: * Adding new classes, non-virtual methods, or free functions. * Adding new static variables to a class. * Removing private static variables (if they are never referenced from an inline method). * Removing non-virtual private methods (if they are never called from an inline method). * Changing the implementation of an inline method (however, this requires recompilation to pick up the new implementation). * Changing an inline method to be non-inline (however, this requires recompilation if the implementation is also changed). * Changing the default arguments of a method (however, this requires recompilation to actually use the new default argument). * Adding or removing friend declarations from a class. * Adding a new enum to a class. * Appending new enumerations to an existing enum. * Using unclaimed remaining bits of a bit field. -------------------------------------------------------------------- P259 8.5.3 Deprecating Functionality // deprecated.h #ifdef GNUC # define DEPRECATED attribute ((deprecated)) #elif defined( MSC VER) # define DEPRECATED declspec(deprecated) #else # define DEPRECATED # pragma message("DEPRECATED is not defined for this compiler") #endif Using this definition, you can mark certain methods as being deprecated in the following way: #include "deprecated.h" #include <string> class MyClass { public: DEPRECATED std::string GetName(); std::string GetFullName(); };

    2012-02-06 16:13:40 回应

Cynosure的其他笔记  · · · · · ·  ( 全部128条 )

夏日再会
1
Alternative Logics. Do Sciences Need Them?
1
Modern Classical Physics
1
中国历代军事战略(上下)
1
费恩曼物理学讲义 (第3卷)(英文版)
1
Introduction to Circle Packing
1
Trick or Truth?
2
Questioning the Foundations of Physics
1
Matrix Computations
1
沉默的大多数
1
要瘦就瘦,要健康就健康
1
Fast Fourier Transform and Its Applications
1
From Groups to Geometry and Back
1
Geometries
1
不锈时光
1
Generations
1
Geometry
1
Effective Modern C++
2
中国国家治理的制度逻辑
1
费恩曼物理学讲义(第2卷)(英文版)
1
费恩曼物理学讲义
1
Differential Geometry
1
Applied Computational Physics
1
Make Your Own Neural Network
1
龙与鹰的帝国
1
巨婴国
1
An Introduction to Manifolds
1
Generalized Curvatures (Geometry and Computing)
1
求索中国
1
Statistical Mechanics
1
Statistical Mechanics
1
The Probability Lifesaver
1
An Introduction to Thermal Physics
1
Modern Approaches to Discrete Curvature
1
Discrete Differential Geometry
1
Visual Complex Analysis
1
The Algorithmic Beauty of Sea Shells
1
The Algorithmic Beauty of Seaweeds, Sponges and Corals
1
The Algorithmic Beauty of Plants
1
致女儿书
1
八十年代中学生
1
毛以后的中国1976-1983
1
The Rise and Fall of the Third Reich
1
Are Leaders Born or Are They Made?
1
光荣与梦想
1
文明的度量
1
Introduction to Quantum Mechanics
1
Waves
1
美国宪政历程
1
Electricity and Magnetism
1
Mechanics (Berkeley Physics Course, Vol. 1)
1
An Introduction to Systems Biology
1
Mathematical Physics
1
Sapiens
1
哲学家们都干了些什么?
1
Fractional Calculus
1
七缀集
1
Polygon Mesh Processing
1
Fractional Calculus View of Complexity
1
Discrete Calculus
1
Perfectly Reasonable Deviations from the Beaten Track
1
A Student's Guide to Maxwell's Equations
1
A Student's Guide to Vectors and Tensors
1
Physics from Symmetry
1
Algebra
1
控制论与科学方法论
1
量子理论
1
Contemporary Abstract Algebra
1
Abstract Algebra(Second Edition)
1
Conscious Loving
1
常微分方程教程
1
Ordinary Differential Equations
1
A Geometric Approach to Differential Forms
1
Differential Geometry of Curves and Surfaces
2
Geometry and the Imagination
1
Differential Geometry
1
Numerical Analysis
1
科学计算导论
1
生物数学趣谈
1
Discovering Modern Set Theory. I: The Basics
1
微积分学教程(第3卷)
3
Historical Dynamics
1
Elementary Calculus
1
超实讲义
1
Vector Calculus
1
微积分学教程(第2卷)
1
文明的进程
1
Digital Lighting and Rendering
1
The VES Handbook of Visual Effects
6
洗脑术
1
The Python Standard Library by Example
1
数学概观
1
数学的统一性
1
好妈妈胜过好老师
1
食品真相大揭秘
1
The Illusion of Life
1
全球通史
5
全球通史
5
变态心理学
2
艺术与癫狂
1
内向者优势
2
维特根斯坦传
1
月亮和六便士
1