It's not your mother's C++ anymore. Manual memory management, tedious loops, difficult-to-use STL algorithms -- are all a thing of the past now. The new C++ 11 standard contains a huge number of improvements to the C++ core language and standard library, and can help C++ developers be more productive.
In this session we will discuss the major features of C++ 11, including lambda functions, type inference for local variables, range-based for loops, smart pointers, and more. We will see how to use these features effectively to modernize your existing C++ programs and how to develop in the modern C++ style.
3. Some History
• First real standard
• What is usually taught at universityC++98
• Bug fixes
C++03
• Smart pointers
• Regexes
• Hashmap and the likes
C++TR1
• We’ll learn today
C++11
• Minor language enhancements
C++14
8. auto Quiz
int 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();
9. Range-Based Loops
vector<MyStr> vec1 {{ 1, "1" },{ 2, "2" }};
Before
for (size_t i = 0; i < vec.size(); ++i) {
const MyStr& s = vec[i];
// do something with s
}
After
for (const auto& s : vec) {
// do something with s
}
10. More Range-Based Loops
map<string,vector<string>> m = ...;
Before
for(map<string,
vector<string>>::iterator pi =
m.begin(); pi != m.end(); ++pi) {
pair<const string,
vector<string>>& pair = *pi;
}
After
for (auto& pair : map) { }
18. 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
22. 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
23. 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?
24. 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);
25. 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
};
28. 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
29. More Stuff You Should Know
Templates
STL algorithms
Rvalue references and move constructors
Variadic templates
32. Exercise
Modernize some real open-source code!
Download code from here:
http://bit.ly/1ryNgji
Go crazy!
Editor's Notes
auto i = getInt(); // int
auto ir = getIntReference(); // int
auto cir = getConstIntReference(); // int
auto& i = getInt(); // doesn’t compile – can’t cast int to int&
auto& ir = getIntReference(); //int &
auto& cir = getConstIntReference(); //const int& - why? Because that’s the way it is
const auto& i = getInt(); // const int& - but don’t do this! – it’s a temporary on the stack. If you pass I to other places you might have a problem!
const auto& ir = getIntReference(); // const int&
const auto& cir = getConstIntReference(); const int&
מה המסקנה מכל זה? שימו את ה-& ואת ה-const בעצמכם איפה שצריך ולא יהיו בלבולים. זה לא מאריך את הקוד באופן משמעותי והקוד ברור!
ואם אנחנו כבר מדברים על דרכים שבהן אפשר לקצר כתיבה של לולאות אז סוף סוף יש לנו סינטקס חדש ללולאות for – סינטקס שמאפשר לנו מעבר ישיר על איברים של container במקום שימוש באיטרטור ואז ב-dereference של האיטרטור הזה.
בדוגמה הזאת קיצרנו קצת את הקוד.