Effective Modern C++ C++ C++ C++11/C++14 C++ Scott Meyers Gerhard Kreuzer Siemens AG Effective Modern C++ Effective Modern C++ Andrei Alexandrescu Facebook Modern C++ Design C++ C++ Nevin Liber DRW Trading Group C++ Bjarne Stroustrup C++11 Effective Modern C++ C++11 C++14 Scott Meyers Cassio Neri FX
Scott Effective C++ C++ C++ Roger Orr OR/2 ISO C++ Effective Modern C++ C++ C++ Scoot 42 Bart Vandewoestyne C++ C++ Scott Effective Modern C++ Damien Watkins CSIRO C++ C++11/14 C++98 C++ Rachel Cheng F5 Networks C++98/03 C++11/14 Scott Effective Modern C++ C++11 Scott Rob Stewart Boost Steering boost.org
C++ C++11 C++ auto for lambda rvalue C++ 0 typedef nullptr alias declaration C++11 C++14 有效率 C++ C++11 C++14 條款 auto const std::unique_ptr Pimpl Idiom lambda std::atomic volatile C++ C++11 C++14
2 C++ C++ ISO C++98 C++03 C++11 C++14 C++98 C++03 C++98 C++11 C++11 C++14 C++14 C++11 superset C++14 C++14 C++ 使用的詞彙 C++ C++98 C++11 C++14 代表的語言版本 C++98 C++03 C++11 C++14 C++14 C++ C++98 C++98 C++03 C++11 lambda C++11 C++14 C++14 C++14 C++11 move semantic rvalue lvalue rvalue move operation lvalue rvalue lvalue rvalue rvalue reference lvalue lvalue rvalue lvalue rvalue T T lvalue T rvalue rvalue lvalue class Widget { public: Widget(Widget&& rhs); // rhs lvalue rvalue }; Widget move constructor rhs rhs lvalue rvalue lvalue
3 Widget Widget rhs right-hand side 搬移操作 move operation 複製操作 copy operation rhs Matrix operator+(const Matrix& lhs, const Matrix& rhs); lhs left-hand side Widget rhs rhs lvalue C++11 variadic... template<typename... Ts> void processvals(const Ts&... params) { } // C++ processvals typename class C++ class 複 copy C++ void somefunc(widget w); // somefunc w // Widget wid; somefunc(wid); // wid Widget // somefunc // w // wid
4 somefunc(std::move(wid)); somefunc // w // wid rvalue lvalue somefunc rvalue lvlaue w Widget 引數 argument 參數 parameter somefunc wid std::move(wid) w lvalue rvalue lvalue 完美轉發 perfect forwarding rvalue lvalue 30 例外安全 基本保證 basic guarantee 強保證 strong guarantee 函式物件 function object operator() functionname(arguments) operator() C C++98 C++11 可呼叫物件 callable object C++ lambda closure lambda closure lambda 函式樣板 function template 樣板函式 template function 樣板類別 類別樣板 C++ 宣告
5 extern int x; class Widget; bool func(const Widget& w); enum class Color; // // // enum 10 int x; class Widget { }; // // bool func(const Widget& w) { return w.size() < 10; } // enum class Color { Yellow, Red, Blue }; enum 章 signature func bool(const Widget&) noexcept constexpr noexcept constexpr 14 15 C++ 停用 deprecate C++11 std::unique_ptr std::auto_ptr C++11 std::auto_ptr 未定義行為 undefined behavior std::vector []
6 iterator data race new 原始指標 raw pointer 智慧型指標 smart pointer operator-> operator* 20 std::weak_ptr ctor dtor ISBN Effective Modern C++ by Scott Meyers (O Reilly). Copyright 2015 Scott Meyers, 978-1-491-90399-5. email emc++@aristeia.com http://www.aristeia.com/bookerrata/ emc++-errata.html
第一章 C++98 C++11 auto decltype C++14 auto decltype C++ auto decltype C++14 decltype(auto) C++ auto decltype 1 C++ auto C++ C++98 C++11 auto auto
8 auto void f(paramtype param); f(expr); f expr T ParamType ParamType const reference qualifier void f(const T& param); // ParamType const T& int x = 0; f(x); // int f T int ParamType const int& T T expr x int T int T expr ParamType ParamType reference type universal reference 24 universal reference lvalue rvalue ParamType universal reference ParamType void f(paramtype param); f(expr); // expr T ParamType
9 ParamType universal reference ParamType universal reference 1. expr 2. expr ParamType pattern-match T void f(t& param); // param int x = 27; const int cx = x; const int& rx = x; // x int // cx const int // rx x const int param T f(x); f(cx); f(rx); // T int param int& // T const int // param const int& // T const int // param const int& cx rx const T const int const int& const reference parameter const reference-to-const const T& constness T rx T rx lvalue rvalue rvalue rvalue
10 f T& const T& cx rx const param const T const template<type T> void f(const T& param); // param int x = 27; const int cx = x; const int& rx = x; f(x); f(cx); f(rx); // T int param const int& // T int param const int& // T int param const int& rx reference-ness param const void f(t* param); int x = 27; const int *px = &x; f(&x); f(px); // param // px x x const int // T int param int* // T const int // param const int* C++ ParamType Universal Reference universal reference rvalue T universal reference T&& lvalue 24
11 expr lvalue T ParamType lvalue T ParamType rvalue lvalue expr rvalue void f(t&& param); int x = 27; const int cx = x; const int& rx = x; f(x); f(cx); f(rx); f(27); // param universal reference // x lvalue T int& // param int& // cx lvalue T const int& // param const int& // rx lvalue T const int& // param const int& // 27 rvalue T int // param int&& 24 universal reference lvalue rvalue universal reference lvalue rvalue universal reference ParamType ParamType pass-by-value void f(t param); // param param expr T
12 1. expr 2. expr expr const volatile volatile 40 int x = 27; const int cx = x; const int& rx = x; f(x); f(cx); f(rx); // T param int // T param int // T param int cx rx const param const param cx rx cx rx 複本 cx rx param param const volatile expr const volatile const const expr const expr const const expr param void f(t param); // param const char* const ptr = "Fun with pointers"; f(ptr); // ptr const const // const char * const ptr const ptr const ptr null const ptr const ptr f param 指標本身 ptr 會以傳值的方式傳入 ptr const param const char* const ptr const ptr param ptr const
13 衰退 const char name[] = "J. P. Briggs"; // name // const char[13] const char * ptrtoname = name; // array name const char* ptrtoname name const char[13] const char* const char[13] array-to-pointer void f(t param); f(name); // T param function parameter void myfunc(int param[]); myfunc void myfunc(int* param); C++ C f T const char* f(name); // name T const char*
14 能夠 參考 f void f(t& param); f(name); f T T const char[13] f const char (&)[13] template<typename T, std::size_t N> // constexpr std::size_t arraysize(t (&)[N]) noexcept // constexpr { // noexcept return N; } 15 constexpr int keyvals[] = { 1, 3, 7, 9, 11, 22, 35 }; // keyvals 7 int mappedvals[arraysize(keyvals)]; // mappedvals 7 C++ std::array std::array<int, arraysize(keyvals)> mappedvals; //mappedvals // size 7 arraysize noexcept 14
15 C++ void somefunc(int, double); // somefunc // void(int, double) void f1(t param); void f2(t& param); f1(somefunc); f2(somefunc); // f1 param // f2 param // param // void (*)(int, double) // param // void (&)(int, double) array-to-pointer function-to-pointer auto universal reference lvalue 4 reference-ness universal reference lvalue by-value const volatile const volatile