C The new standard

Similar documents
C++11 and Compiler Update

CS

C++11/14 Rocks. Clang Edition. Alex Korban

Structured bindings with polymorphic lambas

C++11 Introduction to the New Standard. Alejandro Cabrera February 1, 2012 Florida State University Department of Computer Science

Rvalue References & Move Semantics

Object-Oriented Principles and Practice / C++

Lesson 13 - Vectors Dynamic Data Storage

Advanced Systems Programming

Object-Oriented Principles and Practice / C++

Interview Questions of C++

Object-Oriented Programming

G52CPP C++ Programming Lecture 20

QUIZ. What is wrong with this code that uses default arguments?

Object-Oriented Principles and Practice / C++

Rvalue References, Move Semantics, Universal References

Introduction to Move Semantics in C++ C and C

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

September 10,

Advanced Programming & C++ Language

C++11: 10 Features You Should be Using. Gordon R&D Runtime Engineer Codeplay Software Ltd.

Instantiation of Template class

COMP6771 Advanced C++ Programming

Outline. 1 About the course

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

CS11 Advanced C++ Spring 2018 Lecture 2

EL2310 Scientific Programming

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Overload Resolution. Ansel Sermersheim & Barbara Geller Amsterdam C++ Group March 2019

CPSC 427: Object-Oriented Programming

I m sure you have been annoyed at least once by having to type out types like this:

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

C++ Primer. CS 148 Autumn

Midterm Review. PIC 10B Spring 2018

COEN244: Class & function templates

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Introduction to C++ with content from

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

Domains: Move, Copy & Co

Overload Resolution. Ansel Sermersheim & Barbara Geller ACCU / C++ June 2018

Object Oriented Programming. Solved MCQs - Part 2

Object-Oriented Programming for Scientific Computing

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Advanced C++ Programming Workshop (With C++11, C++14, C++17) & Design Patterns

Type Inference auto for Note: Note:

CS304- Object Oriented Programming LATEST SOLVED MCQS FROM FINALTERM PAPERS. MC

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

A brief introduction to C++

C++ Rvalue References Explained

Introduction to C++11 and its use inside Qt

04-19 Discussion Notes

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Short Notes of CS201

aerix consulting C++11 Library Design Lessons from Boost and the Standard Library

Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates

Tutorial 7. Y. Bernat. Object Oriented Programming 2, Spring Y. Bernat Tutorial 7

Copyie Elesion from the C++11 mass. 9 Sep 2016 Pete Williamson

Absolute C++ Walter Savitch

Qualified std::function signatures

CS201 - Introduction to Programming Glossary By

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Advanced C++ Topics. Alexander Warg, 2017

Overview. 1. Expression Value Categories 2. Rvalue References 3. Templates 4. Miscellaneous Hilarity 2/43

Tokens, Expressions and Control Structures

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

COMP6771 Advanced C++ Programming

STRUCTURING OF PROGRAM

Modern C++, From the Beginning to the Middle. Ansel Sermersheim & Barbara Geller ACCU / C++ November 2017

CS304 Object Oriented Programming

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

An Introduction to Template Metaprogramming

Assignment operator string class c++ Assignment operator string class c++.zip

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Database Systems on Modern CPU Architectures

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Fast Introduction to Object Oriented Programming and C++

Variadic functions: Variadic templates or initializer lists? -- Revision 1

A Crash Course in (Some of) Modern C++

TDDD38 - Advanced programming in C++

l Operators such as =, +, <, can be defined to l The function names are operator followed by the l Otherwise they are like normal member functions:

When we program, we have to deal with errors. Our most basic aim is correctness, but we must

Engineering Tools III: OOP in C++

eingebetteter Systeme

Overview of C Feabhas Ltd 2012

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Cpt S 122 Data Structures. Templates

Logistics. Templates. Plan for today. Logistics. A quick intro to Templates. A quick intro to Templates. Project. Questions? Introduction to Templates

C++ Primer, Fifth Edition

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Other C++11/14 features

Object-Oriented Programming, Iouliia Skliarova

Modernes C++ Träume und Alpträume

IBM Rational Rhapsody TestConductor Add On. Code Coverage Limitations

IS0020 Program Design and Software Tools Midterm, Fall, 2004

New wording for C++0x Lambdas

C++ Important Questions with Answers

Expansion statements. Version history. Introduction. Basic usage

CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1

Transcription:

C++11 - The new standard Lars Kühne Institut für Informatik Lehrstuhl für theoretische Informatik II Friedrich-Schiller-Universität Jena January 16, 2013

Overview A little bit of history: C++ was initially developed by Bjarne Stroustrup in 1979 first ISO Standard in 1998 replaced by the corrigendum C++03 in 2003 (only bug fixes for the implementors) early draft in 2005: technical report 1 (TR1) next (and current) standard release in August, 2011: C++11 Surprisingly, C++11 feels like a new language - the pieces just fit together better. Bjarne Stroustrup

Things you won t miss std::auto ptr: got replaced by std::unique ptr the export keyword was supposed to allow separation of declaration and definition of templates no major compiler support still reserved dynamic exception specifications by default every function can throw anything exceptions specifications allow to restrict that not enforced at compile-time, hard to use correctly

Getting started recognize >> as closing a template declaration, instead of > > new keyword: noexcept void f o o ( ) n o e x c e p t ; a guarantee by the programmer to the compiler to guide optimizations compile-time assertions: static assert Example (static assert) template <typename T> void bar ( const T& x ) { s t a t i c a s s e r t ( s t d : : i s i n t e g r a l <T>:: v a l u e, T must be i n t e g r a l ) ; // do some a r i t h m e t i c... }

default, delete, final and override Example c l a s s A { p u b l i c : A( ) = d e f a u l t ; A( const A&) = d e l e t e ; v i r t u a l void f o o ( ) {} v i r t u a l void bar ( ) f i n a l {} } ; c l a s s B : p u b l i c A f i n a l { p u b l i c : v i r t u a l void f o o ( ) o v e r r i d e ; v i r t u a l void bar ( ) ; // e r r o r } ; c l a s s C : p u b l i c B ; // e r r o r

Extern templates Example in C++03 the compiler is required to instantiate every fully-specified template this may result in long compilation times, if the same template gets instantiated in different translation units // h e a d e r template <typename T> c l a s s Foo {/ compile time e x p e n s i v e d e f i n i t i o n / } ; // s o u r c e 1. o auto x = Foo<double >(); // s o u r c e 2. o > c o m p i l e s f o r a 2nd time! auto x = Foo<double >(); // s o u r c e 2 e x t e r n. o > no i n s t a n t i a t i o n o f Foo<double > extern template c l a s s Foo<double >; auto x = Foo<double >();

The problem with NULL Example as inherited by C, NULL is a Makro usually expanding to 0 void f o o ( i n t ) ; void f o o ( char ) ; // c a l l f o o (NULL ) ; new null-pointer constant: std::nullptr implicitely convertible and comparable to any pointer type not so for integral types, except for bool

The auto keyword old keyword with new meaning tells the compiler to deduce the type of the variable from its initialization i n t x = 4 ; // C++98 auto x = 4 ; // C++11 Serves as a convenience in cases where the type is either hard to write... s t d : : map<i n t, s t d : : v e c t o r <double> > my map ; // C++98 s t d : : map<i n t, s t d : : v e c t o r <double> >:: c o n s t i t e r a t o r i t = my map. b e g i n ( ) ; // C++11 auto i t = my map. c b e g i n ( ) ;

The auto keyword...or hard to know template <c l a s s A, c l a s s B> void f o o ( const A& a, const B& b ) { auto tmp = a b ; } // c a l l i n g the f u n c t i o n f o o ( u, v ) ; Inconvenient C++98 solution: template <c l a s s A, c l a s s B, c l a s s C> void f o o ( const A& a, const B& b ) { C tmp = a b ; } // c a l l i n g the f u n c t i o n foo<w>(u, v ) ;

The auto keyword strips off const-ness and references i n t a = 4 2 ; const i n t& b = a ; auto c = b ; // i n t // e x p l i c i t e l y add on q u a l i f i e r s const auto& d = c ; // c o n s t i n t& pointers, however, are treated as types of their own i n t a p t r = &a ; auto b p t r = a p t r ; // i n t auto c p t r = a p t r ; // i n t

The auto keyword and return-types return-types can be auto as well auto f o o ( ) > i n t { r e t u r n 4 ; } requires the programmer to provide a trailing-return-type template <c l a s s A, c l a s s B> auto f o o ( const A& a, const B& b ) > d e c l t y p e ( a b ) { r e t u r n a b ; } decltype is a new compile-time operator that returns the type of the given expression trailing-return-type especially useful with multiple return-statements (std::common type)

Range-based for-loop Example allows for a more compact way to express an iteration over a range an object is a range, if 1 its type either has the members begin() and end() returning valid iterators 2 or there are functions begin(obj) and end(obj) doing so all STL containers, regular-expression matches and initializer lists are iterable using range-based for loops s t d : : v e c t o r <i n t > v ; //... f i l l v... f o r ( i n t i : v ) s t d : : cout << i << s t d : : e n d l ; f o r ( auto& i : v ) // c o n s t a l s o p o s s i b l e i = i i ;

Lambda functions Example (What s wrong with this picture?) c l a s s SquareFunctor { p u b l i c : template <typename S c a l a r > S c a l a r operator ( ) ( const S c a l a r v a l u e ) { r e t u r n v a l u e v a l u e ; } } ; template <c l a s s I t e r a t o r > void s q u a r e ( I t e r a t o r begin, I t e r a t o r end ) { SquareFunctor mysquarefunctor ; s t d : : f o r e a c h ( begin, end, mysquarefunctor ) ; } a lot of code to square a number required a named entity for a one-liner

Lambda functions Example template <c l a s s I t e r a t o r > void s q u a r e ( I t e r a t o r begin, I t e r a t o r end ) s t d : : f o r e a c h ( begin, end, [ ] ( double v a l u e ) { r e t u r n v a l u e v a l u e ; } ) ; Definition (Lambda) 1 capture []: wildcard & and =, or named variables from scope 2 function body {} 3 no return type: is deduced by the compiler 4 optional: trailing return-type 5 under the hood: the compiler defines and intantiates a full-fledged function object

Lambda functions Example s t d : : map<i n t, s t d : : s t r i n g > mymap ; const i n t i d x = 3 ; // f i l l map... // the c o m p i l e r c a p t u r e s a l l the v a r i a b l e s // used i n the body by r e f e r e n c e auto lambda 1 = [&] ( ) { r e t u r n mymap [ i d x ]. s i z e ( ) > 0 } ; // copy the idx, but c a p t u r e the map by r e f e r e n c e auto lambda 2 = [=,&mymap ] ( ) { r e t u r n mymap [ i d x ]. s i z e ( ) > 0 } ; // c a l l the lambda i f ( lambda 1 ( ) ) e x i t ( EXIT SUCCESS ) ; convenient inline function-declaration C++11-lambdas are not polymorphic (yet)

Rvalue references Example motivation: avoid creating temporaries s t d : : v e c t o r <s t d : : s t r i n g > v ; v. push back ( C++11 ) ; 1 implicitely: calls std::string(const char*) this creates the temporary! 2 calls v.push back(const std::string&) this appends a copy of the temporary 3 implicitely: calls std::string:: string() on the temporary unnecessary constructor and destructor call

Rvalue references idea: reuse the temporary for the final result What makes a temporary/rvalue? Definition (Lvalues and Rvalues) inspired by the side of = they are usually found i n t n = 3 ; // n i s an l v a l u e 5 = n + 7 ; // 5 i s an r v a l u e ( compile time e r r o r ) lvalues (their address) still exists after the statement rvalues do not exist anymore after the statement (even though one might have kept the address)

Rvalue references are defined with && mutable rvalues (exclusively!) bind to rvalue-references special treatment for temporaries binding-rules & const & && const && lvalue x x x x const-lvalue x x rvalue x x x const-rvalue x x

Rvalue references Example (Overload resolution) void f o o ( s t d : : s t r i n g &); void f o o ( const s t d : : s t r i n g &); void f o o ( s t d : : s t r i n g &&); void f o o ( const s t d : : s t r i n g &&); f o o ( l v a l u e ) ; // v o i d f o o ( s t d : : s t r i n g &); f o o ( c o n s t l v a l u e ) ; // v o i d f o o ( c o n s t s t d : : s t r i n g &); f o o ( r v a l u e ) ; // v o i d f o o ( s t d : : s t r i n g &&); f o o ( c o n s t r v a l u e ) ; // v o i d f o o ( c o n s t s t d : : s t r i n g &&); Easy rules: rvalues prefer rvalue-references lvalues prefer lvalue-references must respect const-ness

Rvalue references 1 in practice: const & und && 2 copy-/move-constructors and assignment-operators 3 the compiler recognizes temporaries by resolving them to &&-overloads basis for move semantic Example (Move-semantic methods) c l a s s Foo { p u b l i c : Foo ( ) ; Foo ( const Foo &); Foo& operator=(const Foo &); // move c o n s t r. and assignment op. Foo ( Foo&&); Foo& operator=(foo&&); } ; Foo x ( Foo ( ) ) ; // c a l l s Foo ( Foo&&)

Rvalue references Example (Move Semantics) c l a s s AlsoMovable { } ; c l a s s Moveable { p u b l i c : Moveable ( s t d : : s i z e t s i z e ) : b i g A r r a y (new double [ s i z e ] ) {} } Moveable ( Moveable&& o t h e r ) : b i g A r r a y ( o t h e r. b i g A r r a y ), am ( s t d : : move ( o t h e r. am) ) { o t h e r. b i g A r r a y = s t d : : n u l l p t r ; } Moveable ( ) { d e l e t e b i g A r r a y ; } p r i v a t e double b i g A r r a y ; AlsoMovable am ;

Rvalue references rvalue-references are useful when there is more to move than just basic data types (int, float, pointers,...) big chunks of dynamically allocated memory (moveable) members of some other class type the STL is now move-enabled: container-classes provide move-insertion std::unique ptr is move-only not every moveable can be detected by the compiler Example (user-guided moving) template <c l a s s T> void swap (T& x, T& y ) { T tmp ( s t d : : move ( x ) ) ; x = s t d : : move ( y ) ; y = s t d : : move ( tmp ) ; } std::move turns lvalue-references into rvalue-references

Variadic Templates templates can now have an arbitrary number of arbitrary types Definition (Parameter Pack) indicated with the ellipsis operator:... 1 Template: holds variadic template parameters 2 Function: holds the corresponding function arguments Example template <c l a s s... Types> c l a s s t u p l e ; template <c l a s s T, c l a s s... Args> s t d : : s h a r e d p t r <T> make shared ( Args &&... a r g s ) ; template <c l a s s T, s t d : : s i z e t... Dims> c l a s s MultiDimArray ;

Variadic Templates - Unpacking:... Example template <c l a s s... Args> c l a s s CountArgs ; template <c l a s s T, c l a s s... Args> c l a s s CountArgs<T, Args... > { p u b l i c : const s t a t i c i n t v a l u e = CountArgs<Args... > : : v a l u e + 1 ; } ; // no arguments matches, t o o template <> c l a s s CountArgs<> { p u b l i c : const s t a t i c i n t v a l u e = 0 ; } ; CountArgs<i n t, f l o a t, double >:: v a l u e ; // 3 sizeof...(args) does the same

Variadic Templates - Unpacking patterns Example the...-operator unpacks every element according to the pattern to its left template <c l a s s T, c l a s s... Rest> // add c o n s t and & to each element o f Rest void p r i n t ( const T& obj, const Rest &... r e s t ) { s t d : : cout << o b j ; // don t add a n y t h i n g to r e s t p r i n t ( r e s t... ) ; } template <c l a s s F, c l a s s... Types> void c a l l (F foo, const Types &... t y p e s ) { // t r a n s l a t e s i n t o : f o o ( bar ( t y p e s 1 ), bar ( t y p e s 2 ),... ) f o o ( bar ( t y p e s )... ) ; }

Still more polymorphic function-wrappers type-traits for meta-programming comprehensive number-generation facilitiy regular expressions hash-based unordered containers user-defined literals, initializer lists,... The ISO plans on publishing the next revision C++1y until 2017. There is now an official community website: isocpp.org

Thank you! Questions? Overview of the new C++(C++11) - Scott Meyers C++11 - the final draft (N3337) - ISO cppreference.com, cplusplus.com