What's New in C++ 11/14?
-
Upload
dinazil -
Category
Technology
-
view
448 -
download
2
description
Transcript of What's New in C++ 11/14?
![Page 1: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/1.jpg)
What’s New in C++ 11?Dina Goldshtein
she codes(jlm);July 2014
![Page 2: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/2.jpg)
Agenda C++ History
C++ 11 Usability Standard library
C++ 14
![Page 3: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/3.jpg)
Some History•First real standard
•What is usually taught at universityC++98
•Bug fixesC++03•Smart pointers
•Regexes•Hashmap and the likes
C++TR1
•We’ll learn todayC++11
•Minor language enhancementsC++14
![Page 4: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/4.jpg)
Usability
![Page 5: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/5.jpg)
List Initialization and Uniform Initialization
struct MyStr{ int i; string s;};
Beforevector<MyStr> vec(2);vec[0].i = 1;vec[0].s = "1";vec[1].i = 2;vec[1].s = "2";
AfterMyStr myStr = { 1, "1" };vector<MyStr> vec1 = {{ 1, "1" },{ 2, "2" }};vector<MyStr> vec2{{ 1, "1" },{ 2, "2" }};
![Page 6: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/6.jpg)
Support Uniform Initializationstruct Settings { map<string, int> m;
Settings( initializer_list<pair<string, int>> s) { for (const auto& p : s) { m.insert(p); } }};
![Page 7: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/7.jpg)
Type Inferencestd::map<std::string, std::vector<std::string>> m = ...;
Beforefor(std::map<std::string, std::vector<std::string>>::const_iterator i = m.cbegin(); i != m.cend(); ++i){ }
Afterfor(auto i = m.cbegin(); i != m.cend(); ++i){}
![Page 8: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/8.jpg)
auto Quizint getInt();int& getIntReference();const int& getConstIntReference();
auto i = getInt();auto ir = getIntReference();auto cir = getConstIntReference();
auto& i = getInt();auto& ir = getIntReference();auto& cir = getConstIntReference();
const auto& i = getInt();const auto& ir = getIntReference();const auto& cir = getConstIntReference();
![Page 9: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/9.jpg)
Range-Based Loopsvector<MyStr> vec1 {{ 1, "1" },{ 2, "2" }};
Beforefor (size_t i = 0; i < vec.size(); ++i) { const MyStr& s = vec[i]; // do something with s} Afterfor (const auto& s : vec) { // do something with s}
![Page 10: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/10.jpg)
More Range-Based Loopsmap<string,vector<string>> m = ...;
Beforefor(map<string, vector<string>>::iterator pi = m.begin(); pi != m.end(); ++pi) { pair<const string, vector<string>>& pair = *pi; }
Afterfor (auto& pair : map) { }
![Page 11: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/11.jpg)
Support Range-Based Loopsstruct MyArr { int arr[100]; int size = 0; void push_back(int e) { arr[size++] = e; } int& operator[](int i) { return arr[i]; }};
namespace std { int* begin(MyArr& a){ return &a.arr[0]; } int* end(MyArr& a){ return &a.arr[a.size]; }}
![Page 12: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/12.jpg)
Lambda Functions
][
Capture List
)(
Parameters
}{
Code
;)(
Invocation
![Page 13: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/13.jpg)
Lambdas Shorten Codevector<MyStr> vec = ...;
Beforestruct MyStrComp { bool operator()(const MyStr& s1, const MyStr& s2) const {return s1.i>s2.i;}};sort(vec.begin(), vec.end(), MyStrComp()); Aftersort(vec.begin(), vec.end(), [](const MyStr& s1, const MyStr& s2) { return s1.i > s2.i;});
![Page 14: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/14.jpg)
Reference Capturingvector<int> input{ 1, 2, 3, ... };vector<int> primes;for_each(input.cbegin(), input.cend(), [&primes](int n){ if (isPrime(n)) primes.push_back(n); });for_each(input.cbegin(), input.cend(), [=primes](int n){ if (isPrime(n)) primes.push_back(n); //doesn't compile });
![Page 15: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/15.jpg)
Non-Static Data Initialization
Before
class Complex {public: Complex() { _x = _y = 0; } Complex(double x) :_x(x) { _y = 0; } Complex(double x, double y) : _x(x), _y(y) {}
private: double _x; double _y;};
After
class Complex {public:
Complex(double x) :_x(x) { }
Complex(double x, double y) : _x(x), _y(y) {}
private: double _x = 0; double _y = 0;};
![Page 16: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/16.jpg)
Delegating Constructor
Beforeclass Complex {public: Complex() { init(); } Complex(double x) { init(); _x = x; } Complex(double x, double y) { init(); _x = x; _y = y; }private: double _x; double _y; void init() { _x = _y = 0; }};
Afterclass Complex {public: Complex() : Complex(0) { }
Complex(double x) : Complex(x, 0) { }
Complex(double x, double y) { _x = x; _y = y; }private: double _x; double _y;};
![Page 17: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/17.jpg)
Standard Library
![Page 18: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/18.jpg)
Upgrades to Current Library Significant performance improvements
when previously copying was required Internal memory management Return containers by value Eliminate need to copy elements when
inserting into containers
![Page 19: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/19.jpg)
Oh man…struct _StockThreadParams { map<string, double>& stocks; string stockName; CRITICAL_SECTION* lock;};
DWORD WINAPI _StockThread(LPVOID p) { _StockThreadParams* param = (_StockThreadParams*)p; double stockValue = getStock(param->stockName); EnterCriticalSection(param->lock); param->stocks[param->stockName] = stockValue; LeaveCriticalSection(param->lock); return 0;}
void useOldThread() { CRITICAL_SECTION lock; InitializeCriticalSection(&lock); map<string, double> stocks; _StockThreadParams param1{ stocks, "apple", &lock }; _StockThreadParams param2{ stocks, "microsoft", &lock }; HANDLE threads[] = { CreateThread(NULL, 0, _StockThread, ¶m1, 0, NULL), CreateThread(NULL, 0, _StockThread, ¶m2, 0, NULL) }; WaitForMultipleObjects(2, threads, TRUE, INFINITE); DeleteCriticalSection(&lock);}
![Page 20: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/20.jpg)
Standard Multi-Threadingmutex m;map<string, double> stocks;thread ta([&] { auto val = getStock("apple"); lock_guard<mutex> lg(m); stocks["apple"] = val;});thread tm([&]{ auto val = getStock("microsoft"); lock_guard<mutex> lg(m); stocks["microsoft"] = val;});ta.join(); tm.join();
![Page 21: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/21.jpg)
Tuple Typestuple<int, string, string> httpResponse = getPage("google.com");string body = get<2>(httpResponse);
int status;string header;tie(status, header, ignore) = getPage("yahoo.com");
![Page 22: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/22.jpg)
Smart Pointers No reason to use new or delete any
more... unique_ptr
Use when only a single reference is allowed at the same time
Passing a unique reference discredits the original one
shared_ptr Use when you want to share the instance
between multiple objects weak_ptr
Use to break up cycles
![Page 23: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/23.jpg)
Unique Pointers Passing by value or returning from
function invalidates the previous reference
This is the most common case Any private member Factory methods
Why not simply use values instead?
![Page 24: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/24.jpg)
Shared Pointers Counts references to find out when to
delete a raw pointer
struct employee {/*...*/};struct manager { vector<shared_ptr<employee>> employees;} bob;struct payroll_system { map<shared_ptr<employee>,double> salaries;} payroll;shared_ptr<employee> kate(new employee(/**/));payroll.salaries[kate] = 600000.0;bob.employees.push_back(kate);
![Page 25: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/25.jpg)
Weak Pointers Shared pointers don’t solve the problem
of cyclic references
struct manager { std::vector<shared_ptr<employee>> employees;};struct employee { std::weak_ptr<manager> manager; void request_leave() { if (auto m = manager.lock()) { m->request_leave(*this); } } // m is shared_ptr<manager>, might be null};
![Page 26: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/26.jpg)
Hash Tables Better performance!
std::unordered_set std::unordered_multiset std::unordered_map std::unordered_multimap
![Page 27: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/27.jpg)
For the Mathematicians Random
distributions: uniform_int uniform_real bernoulli binomial geometric negative_binomial poisson exponential Gamma Weibull
extreme_value normal lognormal chi_squared cauchy fisher_f student_t discrete piecewise_constant piecewise_linear
![Page 28: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/28.jpg)
More Goodies in Future C++14 Generic lambda functions auto lambda = [](auto x, auto y) { return x + y; }; Return-type inference auto foo(int i) { if (i == 1) return i; // return int else return foo(i - 1) + i; } STL enhancements:
Shared mutexes and locking Tuple addressing via type
![Page 29: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/29.jpg)
More Stuff You Should Know Templates STL algorithms
Rvalue references and move constructors
Variadic templates
![Page 30: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/30.jpg)
Summary C++ History
C++ 11 Usability Standard library
C++ 14
![Page 31: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/31.jpg)
Questions?
![Page 32: What's New in C++ 11/14?](https://reader037.fdocuments.us/reader037/viewer/2022103000/554f70dcb4c9058a148b52b0/html5/thumbnails/32.jpg)
Exercise Modernize some real open-source code!
Download code from here: http://bit.ly/1ryNgji
Go crazy!