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

Similar documents
C++11 and Compiler Update

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

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

September 10,

Protection Levels and Constructors The 'const' Keyword

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Object-Oriented Programming for Scientific Computing

Introduction to C++11 and its use inside Qt

COMP 2355 Introduction to Systems Programming

Introduction to Programming Using Java (98-388)

Advanced Programming & C++ Language

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

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

Remedial C Now that we can actually use it Pete Williamson

C++ (Non for C Programmer) (BT307) 40 Hours

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

C The new standard

Pointers. Developed By Ms. K.M.Sanghavi

04-19 Discussion Notes

Introduce C# as Object Oriented programming language. Explain, tokens,

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

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

CMSC 132: Object-Oriented Programming II

Rvalue References, Move Semantics, Universal References

C++ Primer, Fifth Edition

Instantiation of Template class

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Object-Oriented Programming for Scientific Computing

Object-Oriented Programming

Objects Managing a Resource

CS

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

Advanced C++ Topics. Alexander Warg, 2017

Dynamic Data Structures

Chapter 12: How to Create and Use Classes

Major Language Changes, pt. 1

Absolute C++ Walter Savitch

Value categories. PRvalues. Lvalues

Object-Oriented Programming

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

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management


Object Oriented Programming. Solved MCQs - Part 2

Modernizing legacy C++ code

Rvalue References & Move Semantics

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

Index. Symbols. bit sequence, 27 ^ (exclusive OR) operator, 30 hexadecimal number, 27 left shift (<<) operator, 31 right shift (>>) operator, 32

Pointers! Arizona State University 1

GEA 2017, Week 4. February 21, 2017

Special Member Functions

Exercise 6.2 A generic container class

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Microsoft Visual C# Step by Step. John Sharp

Advanced Systems Programming

Previous C# Releases. C# 3.0 Language Features. C# 3.0 Features. C# 3.0 Orcas. Local Variables. Language Integrated Query 3/23/2007

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

2 is type double 3 auto i = 1 + 2; // evaluates to an 4 integer, so it is int. 1 auto d = 5.0; // 5.0 is a double literal, so it

CS3157: Advanced Programming. Outline

Contract Programming For C++0x

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Problem Solving with C++

Java Primer 1: Types, Classes and Operators

CSE 307: Principles of Programming Languages

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

CPSC 427: Object-Oriented Programming

COEN244: Class & function templates

Dot Net Framework 4.0: Advanced Microsoft C#.NET Web Development

Expansion statements. Version history. Introduction. Basic usage

CS 162, Lecture 25: Exam II Review. 30 May 2018

Modern and Lucid C++ Advanced for Professional Programmers. Part 1 C Plus Plus Recap. Department I - C Plus Plus

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

Input And Output of C++

C++ Objects Overloading Other C++ Peter Kristensen

A polymorphic value-type for C++

C++14 (Preview) Alisdair Meredith, Library Working Group Chair. Thursday, July 25, 13

Structured bindings with polymorphic lambas

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

CMSC 132: Object-Oriented Programming II

C++\CLI. Jim Fawcett CSE687-OnLine Object Oriented Design Summer 2017

C++ Programming Basics III

Smart Pointers. Some slides from Internet

CSC 210, Exam Two Section February 1999

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Programming in C and C++

Interview Questions of C++

Microsoft. Microsoft Visual C# Step by Step. John Sharp

CPSC 427: Object-Oriented Programming

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

M.C.A DEGREE EXAMINATION,NOVEMBER/DECEMBER 2010 Second Semester MC 9222-OBJECT ORIENTED PROGRAMMING (Regulation 2009)

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Introducing C++ to Java Programmers

CHAPTER 1: INTRODUCING C# 3

COMP6771 Advanced C++ Programming

ECE 449 OOP and Computer Simulation Lecture 14 Final Exam Review

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Topic 9: Type Checking

Topic 9: Type Checking

Transcription:

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

Agenda Default and Deleted Methods Static Assertions Delegated and Inherited Constructors Null Pointer Type Enum Classes Automatic Type Deduction Ranged For Loops Smart Pointers Lambdas and Function Type Move Semantics 2

Default and Deleted Functions New 'default' keyword specifies default constructor or operator Useful when partially implementing the rule of three (or five) 3

Default and Deleted Functions Example: foo uses the default copy constructor and assignment operator 4

Default and Deleted Functions New 'delete' keyword specifies a constructor or operator as unavailable Useful for restricting the way a type can be used 5

Default and Deleted Functions Example: foo is non-copyable 6

Agenda Default and Deleted Methods Static Assertions Delegated and Inherited Constructors Null Pointer Type Enum Classes Automatic Type Deduction Ranged For Loops Smart Pointers Lambdas and Function Type Move Semantics 7

Static Assertions Compile time assertions Useful for generating compiler time errors for templates 8

Example: Static Assertions foo<int, 0> f; would give this compiler error 9

Agenda Default and Deleted Methods Static Assertions Delegated and Inherited Constructors Null Pointer Type Enum Classes Automatic Type Deduction Ranged For Loops Smart Pointers Lambdas and Function Type Move Semantics 10

Delegated and Inherited Constructors Delegated constructors allows one constructor to call another Useful for avoiding code duplication for initialization 11

Delegated and Inherited Constructors Example: foo's default constructor calls foo's second constructor foo() foo(int, int, int) 12

Delegated and Inherited Constructors Inherited constructors allows a class to inherit constructors from its base class Useful for avoiding constructors that simply pass on the same parameters 13

Delegated and Inherited Constructors Example: bar inherits the constructors: bar () bar (int) 14

Agenda Default and Deleted Methods Static Assertions Delegated and Inherited Constructors Null Pointer Type Enum Classes Automatic Type Deduction Ranged For Loops Smart Pointers Lambdas and Function Type Move Semantics 15

Null Pointer Type New 'nullptr' keyword Alias for the 'nullptr_t' type Comparable to any pointer Not implicitly convertible or comparable to integral types, except bool 16

Example: Null Pointer Type Which constructor is called when passing NULL? Which constructor is called when passing nullptr? 17

Agenda Default and Deleted Methods Static Assertions Delegated and Inherited Constructors Null Pointer Type Enum Classes Automatic Type Deduction Ranged For Loops Smart Pointers Lambdas and Function Type Move Semantics 18

Enum Classes Improvement on traditional enums Allows forward declarations Does not pollute top level namespace Not implicitly convertible to integers Can specify the element size 19

Enum Classes Example: Each element of the enum is an integer. animal_type animaltype = animal_type::dog 20

Agenda Default and Deleted Methods Static Assertions Delegated and Inherited Constructors Null Pointer Type Enum Classes Automatic Type Deduction Ranged For Loops Smart Pointers Lambdas and Function Type Move Semantics 21

Automatic Type Deduction New 'auto' keyword allows compile type deduction Useful when a type is very complex such as iterators or functions 22

Example: Automatic Type Deduction 23

Agenda Default and Deleted Methods Static Assertions Delegated and Inherited Constructors Null Pointer Type Enum Classes Automatic Type Deduction Ranged For Loops Smart Pointers Lambdas and Function Type Move Semantics 24

Ranged For Loops Simpler syntax for iterable types Can be used on any type that has either: begin() and end() methods begin(std::vector) and end(std::vector) functions 25

Example: Ranged For Loops It is also possible to use auto as the type for i 26

Agenda Default and Deleted Methods Static Assertions Delegated and Inherited Constructors Null Pointer Type Enum Classes Automatic Type Deduction Ranged For Loops Smart Pointers Lambdas and Function Type Move Semantics 27

Smart Pointers Standard library template pointer classes Aims to solve the problems associated with raw pointers management shared_ptr reference counts the pointer unique_ptr only allows a single copy of the pointer 28

Smart Pointers Example: Oops, the destructor isn't deleting the raw pointer! 29

Smart Pointers Example: The shared_ptr handles deleting the pointer automatically 30

Agenda Default and Deleted Methods Static Assertions Delegated and Inherited Constructors Null Pointer Type Enum Classes Automatic Type Deduction Ranged For Loops Smart Pointers Lambdas and Function Type Move Semantics 31

Lambdas and Function Type Anonymous functions! [] - variable capture ([&], [=], [this]) () - parameters {} - function body Function type 'std::function<void(int)>' 32

Lambdas and Function Type Example: 33

Agenda Default and Deleted Methods Static Assertions Delegated and Inherited Constructors Null Pointer Type Enum Classes Automatic Type Deduction Ranged For Loops Smart Pointers Lambdas and Function Type Move Semantics 34

Move Semantics L-value An expression with identity, that has address-able memory Variables, pointers, references, parameters R-value An expression with no identity, that does not have address-able memory Literals, temporaries 35

Move Semantics Example: These are all l-values 36

Move Semantics Example: These are all r-values 37

Move Semantics L-value reference int &r; R-value reference int &&r; std::move() Prolongs an r-value reference 38

Example: Move Semantics When make_foo returns, a temporary foo object is created on the stack which is used to construct f 39

Example: Move Semantics By using std::move() the move constructor for foo is triggered therefore avoiding the copy Important to note that when using move semantics, the previous object becomes invalid 40

What Next? C++11 is awesome Try it out There are many other features C++14 is now out Try that out too 41