Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using C++ The Weird Way Something about c++11 & OOP tricks

Similar presentations


Presentation on theme: "Using C++ The Weird Way Something about c++11 & OOP tricks"— Presentation transcript:

1 Using C++ The Weird Way Something about c++11 & OOP tricks
卢思迪 ACM班

2 类型推导 Auto Decltype Decltype((x)) & Decltype(x)

3 外部模板 extern template class std::vector<_type>;
//表示不在这个binary里面实例化类模板

4 in-range for In python: for i in <iterable object>: Now this:
Int arr[5] = {1, 2, 3, 4, 5}; for (int & x : arr){};

5 右值引用与移动语义 Container(Container && src); template<typename _type>
std::move(_type && _obj);

6 nullptr void F(int a){ cout<<a<<endl; } void F(int *p){
assert(p != NULL); cout<< p <<endl; int main(){ int *p = nullptr; int *q = NULL; bool equal = ( p == q ); // equal的值为true,说明p和q都是空指针 int a = nullptr; // 编译失败,nullptr不能转为int F(0); // 在C++98中编译失败,有二义性;在C++11中调用F(int) F(nullptr); return 0;

7 Lambda Expression & functor
<functional> Operator() lambda function: #include <iostream> using namespace std; template <typename s_t> int foo(s_t f) { int x = 23, y = 5; return f(3); } int main() int x = 2, y = 4; auto f = [x, y](int p) -> int {return x * y * p;}; cout << f(3) << endl; x = 3; cout << foo(f) << endl; return 0;

8 Anonymous Struct & Initializer_list
auto i_list = {1, 2, 3, 4}; union TShort { public: struct {byte al, ah;}; byte digit[2]; halfword ax; short sax; TShort(); TShort(short _sax); TShort(byte _al, byte _ah); };

9 单例类与funclass 单例类:用来写Java味儿的C++以及一些结构性的代码 funclass: class print { };
public: print(int x) { cout << x << endl} };

10 wrapper class 例1 带下标检查的访问器
template <typename ref_type, int i> class accessor { ref_type * ptr; public: operator ref_type*() { return *ptr}; ref_type & operator[](size_t x) if (size_t > i || size_t < 1) throw std::runtime_error(“index out”); return ptr[x];} } };

11 例2:智能指针 unique_ptr shared_ptr weak_ptr

12 std::thread #include <stdio.h> #include <stdlib.h>
#include <iostream> #include <thread> #include <string> using namespace std; int main(int argc, const char *argv[]) { std::thread t([](){while(1) cout << "test1" << endl;}), t2([](){while(1) cout << "test2" << endl;}); t.join(); t2.join(); return 0; }

13 Q&A


Download ppt "Using C++ The Weird Way Something about c++11 & OOP tricks"

Similar presentations


Ads by Google