c++11 en 12 exemples simples
TRANSCRIPT
C++11Un aperçu en 12 exemples
2
En bref
• Syntaxe pratique
• Plus clair et plus sûr
• Nouvelles fonctionnalités
• Stl améliorée
3
Syntaxe pratique #1 : auto
auto i = 42; // i is an intauto p = new Foo(42); // p is a Foo*
C++11
4
Syntaxe pratique #1 : auto
for(vector<int>::const_iterator it = v.begin(); it != v.end(); ++it){
…}
for(auto it = v.begin(); it != v.end(); ++it){
…}
C++11
C++03
5
Syntaxe pratique #2 : listes d’initialisateurs
std::vector<int> nombres;nombres.push_back(1);nombres.push_back(2);nombres.push_back(3);nombres.push_back(4);nombres.push_back(5);
std::vector<int> nombres = { 1, 2, 3, 4, 5 }; C++11
C++03
6
Syntaxe pratique #3 : foreach
vector<int> nombres;
for(int &element : nombres)
{
cout << element << endl;
}
vector<int> nombres;for(int i = 0 ; i < nombres.size() ; i++){
int & element = nombres[i] ;cout << element << endl;
}
C++11
C++03
7
Plus clair et plus sûr #1 : héritage virtuel
class A{ virtual void f() const;};
class B : public A{ void f(); // intentionally not overriding?
};
C++03
8
Plus clair et plus sûr #1 : héritage virtuel
class A{ virtual void f() const;};
class B : public A{ virtual void f() override; // error, f does not override};
C++11
9
Plus clair et plus sûr #1 : héritage virtuel
class A{ virtual void f() final;};
class B : public A{ virtual void f(); // error, f is final};
C++11
10
Plus clair et plus sûr #2 : enums
enum Selection{
None,Single,Multiple
};enum Border{
None,Flat,Raised
};
Selection sel = None; // name clash
C++03
11
Plus clair et plus sûr #2 : enums
enum class Selection{
None,Single,Multiple
};enum class Border{
None,Flat,Raised
};
Selection sel = Selection::None;
C++11
12
Plus clair et plus sûr #3 : nullptr
int* p1 = NULL; // macroint* p2(0);
void f(int n);void f (char *s); f( NULL ); // which function is called?
int* p2 = nullptr;
int i = nullptr; // error
C++11
C++03
13
Nouvelle fonctionnalité #1 : lambdas
void f(std::vector<int>& v){ struct DoSomething { void operator()(int) { // do something } }; DoSomething do_something;
std::for_each(v.begin(), v.end(), do_something);}
C++03
14
Nouvelle fonctionnalité #1 : lambdas
void f (std::vector<int>& v){ std::for_each(v.begin(), v.end(), [](int) { /* do something here*/ });}
void f(std::vector<int>& v){ struct DoSomething { void operator()(int) { // do something } }; DoSomething do_something;
std::for_each(v.begin(), v.end(), do_something);}
C++11
C++03
15
Nouvelle fonctionnalité #2 : move and rvalues
template <class T> swap(T& a, T& b){ T tmp(std::move(a)); a = std::move(b); b = std::move(tmp);}
template <class T> swap(T& a, T& b){ T tmp(a); a = b; b = tmp;}
C++11
C++03
16
Nv. fonctionnalité #3 : delegated constructorclass Unloader{public: Unloader(std::string name, int arms_number)
: m_name(name), m_arms_number(arms_number) { } Unloader()
: m_name(""), m_arms_number(0) // duplicated code { }
protected: const std::string m_name; const int m_arms_number;};
C++03
17
Nv. fonctionnalité #3 : delegated constructor
class Unloader{public: Unloader(std::string name, int arms_number)
: m_name(name), m_arms_number(arms_number) { }
Unloader() : Unloader("", 0) { }
protected: const std::string m_name; const int m_arms_number;};
C++11
18
Nouvelles fonctionnalités : et aussi …
• Constant expressions
• Variadic templates
• Assertions statiques
19
Std améliorée #1 : smart pointers
shared_ptr<Foo> sptr1( new Foo ); shared_ptr<Foo> sptr2 = sptr1; // reference counter incremented std::unique_ptr<Foo> uptr1(new Foo); std::unique_ptr<Foo> uptr2(std::move(uptr1)); // p2 owns Foo std::weak_ptr<Foo> wptr1 = sptr1; // wptr1.lock() == sptr1 sptr1.reset( new Foo ); // wptr1.expired() == true
C++11
20
Std améliorée #2 : regex
std::string names[] = {"foo.txt", "foo.bat"};std::regex txt_regex("[a-z]+\\.txt");
for (const auto &n: names) { std::cout << n<< ": " << std::regex_match(fname, txt_regex) << " / ";} // Prints foo.txt: 1 / foo.bat: 0
C++11
21
Std améliorée #3 : threading
void call_from_thread() { std::cout << "Hello world" << std::endl;}
int main(){ std::thread t1(call_from_thread); // thread launch t1.join(); // join thread with main thread return 0;}
C++11
22
Std améliorée : et aussi …
• Performances améliorées
• Chrono
• Nouveaux conteneurs, nouveaux algorithmes
• Tuples
23
A vos questions