Rock Hard: C++ Evolving
description
Transcript of Rock Hard: C++ Evolving
![Page 1: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/1.jpg)
Rock Hard: C++ EvolvingBoris Jabes
blogs.msdn.com/vcblog (@visualc)
![Page 2: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/2.jpg)
C++0x to become C++11FDIS Submitted to ISO
![Page 3: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/3.jpg)
![Page 4: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/4.jpg)
Power & Performance on any Platform“Higher-level style of programming more natural than before and as
efficient as ever.”
“If you timidly approach C++ as just a better C or as an object-oriented language, you are going to miss the point.”
![Page 5: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/5.jpg)
![Page 6: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/6.jpg)
Programming with ValuesValue = No State || Deep Copy
Requires a little bit of forethought
![Page 7: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/7.jpg)
Silly?
int Foo(int x){
// check for nullif (x == null)
// throw exception
...}
Bar Foo(Bar x){
// check for nullif (x == null)
// throw exception
...}
![Page 8: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/8.jpg)
![Page 9: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/9.jpg)
POD
struct fighter {string name;int health;
}; // just works!
int main() {fighter bart = { “bart”, 25 };fighter lisa = bart; // works just like
int!return 0;
}
![Page 10: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/10.jpg)
Dynamic Data
struct fighter {T* data;string name;int health;fighter(string s, int h) : name(s), health(h), data(new T[1000]) {}
};
int main() {fighter bart = { “bart”, 25 };fighter lisa = bart; // shallow copyreturn 0;
}
![Page 11: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/11.jpg)
Dynamic Data
struct fighter {T* data;string name;int health;fighter(string s, int h) :
name(s), health(h), data(new T[1000]) {}fighter(const fighter& o) :
name(o.name), health(o.health), data(new T[1000]) {std::copy(o.data,o.data+1000,data);
}};
int main() {fighter bart = { “bart”, 25 };fighter kirk(get_fighter()); // this worksfighter lisa = bart; // this doesn’treturn 0;
}
![Page 12: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/12.jpg)
Dynamic Datastruct fighter {
T* data;string name;int health;fighter(string s, int h) : name(s), health(h), data(new T[HUGE]) {}fighter(const fighter& o) :name(o.name), health(o.health), data(new T[HUGE]) {std::copy(o.data,o.data+HUGE,data);}void swap(fighter& left, fighter& right) {std::swap(left.name,right.name);std::swap(left.health,right.health);std::swap(left.data,right.data); // swap head pointer}fighter& operator=(fighter o) {swap(*this,o);return *this;}
};
![Page 13: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/13.jpg)
Expensive Copystruct fighter {
T* data;string name;int health;fighter(string s, int h) : name(s), health(h), data(new T[HUGE]) {}fighter(const fighter& o) :name(o.name), health(o.health), data(new T[HUGE]) {std::copy(o.data,o.data+HUGE,data);}friend void swap(fighter& left, fighter& right) {std::swap(left.name,right.name);std::swap(left.health,right.health);std::swap(left.data,right.data); // swap head pointer}fighter& operator=(fighter o) {swap(*this,o);return *this;}fighter(fighter&& o) : data(nullptr) {swap(*this,o);}
};
![Page 14: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/14.jpg)
![Page 15: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/15.jpg)
![Page 16: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/16.jpg)
![Page 17: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/17.jpg)
![Page 18: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/18.jpg)
![Page 19: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/19.jpg)
Destructors + RAII = The Best of C++Determinism for Resources
Cache-LocalitySeamless with Exceptions
Zero Burden on API Consumer
![Page 20: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/20.jpg)
Higher-Order ProgrammingMulti-Paradigm
GenericControl
And Yet… Efficient
![Page 21: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/21.jpg)
Control in ContextLambdas
![Page 22: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/22.jpg)
C#
List<Action> actions = new List<Action>();
for (int counter = 0; counter < 10; counter++){
actions.Add(() => Console.WriteLine(counter));}
foreach (Action action in actions){
action();}
![Page 23: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/23.jpg)
C#
List<Action> actions = new List<Action>();
for (int counter = 0; counter < 10; counter++){
int copy = counter;actions.Add(() =>
Console.WriteLine(copy));}
foreach (Action action in actions){
action();}
![Page 24: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/24.jpg)
C++
vector<function<void()> actions;
for (int counter = 0; counter < 10; counter++){
actions.push_back([&] {cout << counter << endl;});
}
for(int i=0; i<actions.size(); ++i){
actions[i]();}
![Page 25: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/25.jpg)
C++
vector<function<void()> actions;
for (int counter = 0; counter < 10; counter++){
actions.push_back([=] {cout << counter << endl;});
}
for(int i=0; i<actions.size(); ++i){
actions[i]();}
![Page 26: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/26.jpg)
![Page 27: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/27.jpg)
![Page 28: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/28.jpg)
C++11 Coming Soon to a Compiler Near You
Modern C++ != “C with Classes”C++11 = Expressive Without Sacrifice
C++11 is ConcurrentGo Forth & Be Merry
![Page 29: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/29.jpg)
Stay up to date with MSDN Belux
• Register for our newsletters and stay up to date:http://www.msdn-newsletters.be• Technical updates• Event announcements and registration• Top downloads
• Follow our bloghttp://blogs.msdn.com/belux
• Join us on Facebookhttp://www.facebook.com/msdnbehttp://www.facebook.com/msdnbelux
• LinkedIn: http://linkd.in/msdnbelux/ • Twitter: @msdnbelux
Download MSDN/TechNet Desktop Gadget
http://bit.ly/msdntngadget
![Page 30: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/30.jpg)
TechDays 2011 On-Demand
• Watch this session on-demand via Channel9http://channel9.msdn.com/belux
• Download to your favorite MP3 or video player• Get access to slides and recommended resources by the speakers
![Page 31: Rock Hard: C++ Evolving](https://reader035.fdocuments.us/reader035/viewer/2022062218/56816571550346895dd80877/html5/thumbnails/31.jpg)
THANK YOU