introduction to c 11 and its use inside qt
play

Introduction to C++11 and its use inside Qt Olivier Goffart - PowerPoint PPT Presentation

Introduction to C++11 and its use inside Qt Olivier Goffart February 2013 1/43 Introduction to C++11 and its use inside Qt About Me http://woboq.com http://code.woboq.org 2/43 Introduction to C++11 and its use inside Qt History 1979: C


  1. Introduction to C++11 and its use inside Qt Olivier Goffart February 2013 1/43 Introduction to C++11 and its use inside Qt

  2. About Me http://woboq.com http://code.woboq.org 2/43 Introduction to C++11 and its use inside Qt

  3. History 1979: C With classes 1983: C++ 1991: Qt development begins (Qt 1.0 in 1995) 1998: C++98 - First standardized C++ 2003: C++03 - Only few corrections 2005: Qt 4.0 2011: C++11 2012: Qt 5.0 3/43 Introduction to C++11 and its use inside Qt

  4. Goals of C++11 From the C++11 FAQ: Make C++ a better language for systems programming and library building that is, to build directly on C++’s contributions to programming, rather than providing specialized facilities for a particular sub-community (e.g. numeric computation or Windows-style application development). Make C++ easier to teach and learn through increased uniformity, stronger guarantees, and facilities supportive of novices (there will always be more novices than experts). 4/43 Introduction to C++11 and its use inside Qt

  5. Rules of Thumbs From the C++11 FAQ: Maintain stability and compatibility Prefer libraries to language extensions Prefer generality to specialization Support both experts and novices Increase type safety Improve performance and ability to work directly with hardware Fit into the real world 5/43 Introduction to C++11 and its use inside Qt

  6. Example QString processString( const QString &str) 1 { 2 State {Ok , Cancel }; enum 3 QVector <QPair <State , QString >> stack; 4 stack.append ({ State ::Ok , str }); 5 //... 6 } 7 What is specific to C++11 in this code? 6/43 Introduction to C++11 and its use inside Qt

  7. Example QString processString( const QString &str) 1 { 2 State {Ok , Cancel }; enum 3 QVector <QPair <State , QString >> stack; 4 stack.append ({ State ::Ok , str }); 5 //... 6 } 7 What is specific to C++11 in this code? use of bracket initilizer 6/43 Introduction to C++11 and its use inside Qt

  8. Example QString processString( const QString &str) 1 { 2 State {Ok , Cancel }; enum 3 QVector <QPair <State , QString >> stack; 4 stack.append ({ State ::Ok , str }); 5 //... 6 } 7 What is specific to C++11 in this code? use of bracket initilizer use of >> without a space 6/43 Introduction to C++11 and its use inside Qt

  9. Example QString processString( const QString &str) 1 { 2 State {Ok , Cancel }; enum 3 QVector <QPair <State , QString >> stack; 4 stack.append ({ State ::Ok , str }); 5 //... 6 } 7 What is specific to C++11 in this code? use of bracket initilizer use of >> without a space Scoped State::Ok 6/43 Introduction to C++11 and its use inside Qt

  10. Example QString processString( const QString &str) 1 { 2 State {Ok , Cancel }; enum 3 QVector <QPair <State , QString >> stack; 4 stack.append ({ State ::Ok , str }); 5 //... 6 } 7 What is specific to C++11 in this code? use of bracket initilizer use of >> without a space Scoped State::Ok use of local type State as template parameter 6/43 Introduction to C++11 and its use inside Qt

  11. New Features 7/43 Introduction to C++11 and its use inside Qt

  12. auto QString Obj::foo( int blah) 1 { 2 QHash < int , QString > dict = bar (); 3 auto it = dict.find(blah ); 4 //... 5 } 6 8/43 Introduction to C++11 and its use inside Qt

  13. Member Initializers class X { 1 // ... 2 private : 3 bool m_enabled = false ; 4 int m_state = 0; 5 QByteArray m_name = "None"; 6 } 7 9/43 Introduction to C++11 and its use inside Qt

  14. Range Based for bool Obj::foo( const QStringList &list) 1 { 2 for ( const QString &it : list) { 3 //... 4 } 5 } 6 10/43 Introduction to C++11 and its use inside Qt

  15. Uniform Initialisation In C++98 int b(1); // variable definition 1 QVariant c(); // function declaration 2 QVariant d(QString ()); // oops 3 4 QPoint p(1 ,2); 5 int i = {2}; //yes , you can do that 6 QString a[] = { "foo", "bar" }; // ok 7 // QVector <QString > v = { "foo", "bar" }; // error 8 11/43 Introduction to C++11 and its use inside Qt

  16. Uniform Initialisation In C++11 int b{1}; // variable definition 1 QVariant c{}; // default constructor 2 QVariant d{QString ()}; // Works as expected 3 4 QPoint p{1 ,2}; 5 int i = {2}; // = is optional 6 QString a[] { "foo", "bar" }; 7 QVector <QString > v = { "foo", "bar" }; // works 8 12/43 Introduction to C++11 and its use inside Qt

  17. Uniform Initialisation In C++11 struct X { int a, b; }; 1 X x1 = X{1 ,2}; 2 X x2 = {1 ,2}; // the = is optional 3 X* p = new X{1 ,2}; 4 5 struct D : X { 6 D( int x, int y, int z) : X{x,y}, c{z} 7 { /* ... */ }; 8 int c; 9 }; 10 13/43 Introduction to C++11 and its use inside Qt

  18. Uniform Initialisation In C++11 X foo( const X &a) { 1 X b{a}; 2 return {2 ,3}; 3 } 4 foo ({4 ,5}); 5 6 X x5{4.2, 4.5} // error: narrowing 7 8 // using initializer_list constructor (from Qt 4.8) 9 QVector <X> { {1,2}, {2,4}, {4,5} }; 10 14/43 Introduction to C++11 and its use inside Qt

  19. initializer_list #ifdef Q_COMPILER_INITIALIZER_LISTS 1 template < typename T> 2 QVector <T>:: QVector(std:: initializer_list <T> args) 3 { 4 d = Data :: allocate(args.size ()); 5 copyConstruct(args.begin (), args.end(), d->begin ()); 6 d->size = args.size (); 7 } 8 #endif 9 15/43 Introduction to C++11 and its use inside Qt

  20. C K&R syntax int plus(x, y) 1 int x; 2 int y; 3 { 4 return x + y; 5 } 6 16/43 Introduction to C++11 and its use inside Qt

  21. Lambda λ 17/43 Introduction to C++11 and its use inside Qt

  22. Lambda [foo] (int a) -> int { return a + foo; } Capture: Variables that you capture Parametter list: The perametters of the function Return type (optional) Function body 18/43 Introduction to C++11 and its use inside Qt

  23. Lambda [foo] (int a) -> int { return a + foo; } struct { double foo; int operator()(int a) { return a + foo; } } 18/43 Introduction to C++11 and its use inside Qt

  24. Lambda Example QList < int > f( const QList < int > &list , double foo) 1 { 2 functor = [foo]( int a) { return a + foo; }; auto 3 QtConcurrent :: mapped(list , functor ); return 4 } 5 19/43 Introduction to C++11 and its use inside Qt

  25. Lambda capture int a{1}, b{2}, c{3}; 1 2 // ’a’ by value , ’b’ by reference 3 auto f1 = [a, &b]() { b = a; }; 4 5 // everything by reference 6 auto f2 = [&]() { b = a; }; 7 8 // everything by value 9 auto f3 = [=]() { return a + c; }; 10 11 // everything by value , ’b’ by reference 12 auto f4 = [=,&b]() { b = a + c; }; 13 20/43 Introduction to C++11 and its use inside Qt

  26. Lambda Example (Qt5) void Doc:: saveDocument () { 1 QFileDialog *dlg = new QFileDialog (); 2 dlg ->open (); 3 QObject :: connect(dlg , &QDialog :: finished , 4 [dlg , this ]( int result) { 5 if (result) { 6 QFile file(dlg ->selectedFiles (). first ()); 7 // ... 8 } 9 dlg ->deleteLater (); 10 }); 11 } 12 21/43 Introduction to C++11 and its use inside Qt

  27. Function Declaration struct Foo { 1 State { /*...*/ }; enum 2 /*...*/ 3 State func(State ); 4 }; 5 6 // Error 7 State Foo:: func(State arg) { /*...*/ } 8 22/43 Introduction to C++11 and its use inside Qt

  28. Function Declaration struct Foo { 1 State { /*...*/ }; enum 2 /*...*/ 3 State func(State ); 4 }; 5 6 // Error 7 State Foo:: func(State arg) { /*...*/ } 8 Alternative function syntax auto Foo:: func(State arg) -> State 1 { /* ... */ } 2 22/43 Introduction to C++11 and its use inside Qt

  29. decltype template < typename A, typename B> 1 dotProduct(A vect_a , B vect_b) auto 2 -> decltype (vect_a [0]* vect_b [0]) 3 { /*...*/ } 4 5 6 7 QList < int > a{/*...*/}; 8 QVector < float > b{/*...*/}; 9 auto p = dotProduct(a, b); 10 23/43 Introduction to C++11 and its use inside Qt

  30. Default and Deleted function MyObject { struct 1 virtual ~MyObject () = default ; 2 MyObject( int ); 3 MyObject () = default ; 4 int someFunction(QObject *); 5 int someFunction(QWidget *) = delete ; 6 }; 7 #define Q_DISABLE_COPY(Class) \ 1 Class( const Class &) = delete ; \ 2 Class & operator =( const Class &) = delete ; 3 24/43 Introduction to C++11 and its use inside Qt

  31. Q_DECL_DELETE struct MyObject { 1 myFunction( int ); void 2 // ... 3 private : 4 myFunction( bool ) Q_DECL_DELETE ; void 5 }; 6 25/43 Introduction to C++11 and its use inside Qt

  32. Rvalue References string { struct 1 char *data; 2 string( const char *str) : data(strdup(str)) {} 3 string( const string &o) : data(strdup(o.data )) {} 4 ~string () { free(data ); } 5 string & operator =( const string &o) { 6 free(data ); data = strdup(o.data) 7 return * this ; 8 } 9 }; 10 11 template < class T> swap(T &a, T &b) { 12 T tmp(a); a = b; b = tmp; 13 } 14 26/43 Introduction to C++11 and its use inside Qt

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend