Major Language Changes, pt. 1

Similar documents
Major Language Changes, pt. 1

Boost.Compute. A C++ library for GPU computing. Kyle Lutz

To use various types of iterators with the STL algorithms ( ). To use Boolean functions to specify criteria for STL algorithms ( 23.8).

Lecture 21 Standard Template Library. A simple, but very limited, view of STL is the generality that using template functions provides.

C++ Standard Template Library

Parallelism and Concurrency in C++17 and C++20. Rainer Grimm Training, Coaching and, Technology Consulting

Distributed Real-Time Control Systems. Lecture 16 C++ Libraries Templates

Concurrency and Parallelism with C++17 and C++20. Rainer Grimm Training, Coaching and, Technology Consulting

New Iterator Concepts

Working Draft, Technical Specification for C++ Extensions for Parallelism, Revision 1

Parallelism in C++ J. Daniel Garcia. Universidad Carlos III de Madrid. November 23, 2018

Programming Languages Technical Specification for C++ Extensions for Parallelism

Parallel Programming with OpenMP and Modern C++ Alternatives

C and C++ Courses. C Language

C++ - parallelization and synchronization. Jakub Yaghob Martin Kruliš

Data_Structures - Hackveda

Generic Programming with JGL 4

C++ - parallelization and synchronization. David Bednárek Jakub Yaghob Filip Zavoral

C++0x. Handout #34 June 2, CS106L Spring Introduction

Functors. Cristian Cibils

COPYRIGHTED MATERIAL. Index SYMBOLS. Index

Contents. 2 Introduction to C++ Programming,

Deitel Series Page How To Program Series

C++ How To Program 10 th Edition. Table of Contents

GJL: The Generic Java Library

Scientific programming (in C++)

Chapters and Appendices F J are PDF documents posted online at the book s Companion Website, which is accessible from.

Foundations of Programming, Volume I, Linear structures

COEN244: Class & function templates

STL. Zoltán Porkoláb: C++11/14 1

Recap: Can Specialize STL Algorithms. Recap: Can Specialize STL Algorithms. C++11 Facilities to Simplify Supporting Code

CSE100. Advanced Data Structures. Lecture 4. (Based on Paul Kube course materials)

CS 247: Software Engineering Principles. C++ lambdas, bind, mem_fn. U Waterloo CS247 (Spring 2017)

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation

CSE 100: C++ TEMPLATES AND ITERATORS

I/O and STL Algorithms

CSE 100: C++ TEMPLATES AND ITERATORS

Bigger Better More. The new C++ Standard Library. Thomas Witt April Copyright 2009 Thomas Witt

19.1 The Standard Template Library

C++ Standard Template Library

MODULE 35 --THE STL-- ALGORITHM PART III

Advanced C++ STL. Tony Wong

GridKa School 2013: Effective Analysis C++ Standard Template Library

Exceptions, Templates, and the STL

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

STL Containers, Part II

TEMPLATES AND ITERATORS

CS11 Introduction to C++ Spring Lecture 8

COPYRIGHTED MATERIAL INDEX. Numbers & Symbols. 16-bit characters, bit characters, 10

Introduction to C++11 and its use inside Qt

More STL algorithms (revision 2)

Unifying Operator and Function-Object Variants of Standard Library Algorithms

Lecture 8. Exceptions, Constructor, Templates TDDD86: DALP. Content. Contents Exceptions

COPYRIGHTED MATERIAL INDEX. Symbols

Efficient C++ Programming and Memory Management

CPSC 427a: Object-Oriented Programming

Computational Physics

Unit 4 Basic Collections

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

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

CSE 100: C++ TEMPLATES AND ITERATORS

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

[LABORATOR DE PROGRAMARE C++] Aritoni Ovidiu. Algoritmi STL

The Standard Template Library. An introduction

Chapter 5. The Standard Template Library.

Standard Library Reference

Lecture-5. STL Containers & Iterators

Function Templates. Consider the following function:

Templates C++ Primer Chapter 16

MODULE 37 --THE STL-- ALGORITHM PART V

STL Algorithms, Part I

More STL algorithms. Design Decisions. Doc No: N2569= Reply to: Matt Austern Date:

CS197c: Programming in C++

Teaching with the STL

Dynamic Data Structures

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

CS11 Advanced C++ Spring 2018 Lecture 2

1. The term STL stands for?

Ranges Design Cleanup

use static size for this buffer

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science

Templates & the STL. CS 2308 :: Fall 2015 Molly O'Neil

Contest programming. Linear data structures. Maksym Planeta

Where do we go from here?

C++11 and Compiler Update

G52CPP C++ Programming Lecture 18

Boost and C++11. Cristian Cibils

Outline. Function calls and results Returning objects by value. return value optimization (RVO) Call by reference or by value?

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Chapters and Appendices F I are PDF documents posted online at the book s Companion Website (located at

Introduction to GIL, Boost and Generic Programming

C++ Programming Lecture 2 Software Engineering Group

CS

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

Improving the Return Value of Erase-Like Algorithms

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

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

Templates and Vectors

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

STANDARD TEMPLATE LIBRARY. Developed By Mrs. K.M.Sanghavi

Transcription:

C++0x

What is C++0x? Updated version of C++ language. Addresses unresolved problems in C++03. Almost completely backwards compatible. Greatly increases expressiveness (and complexity!) of language. Greatly reduces difficulty of use.

Major Language Changes, pt. 1

Without C++0x: void PrintMultiMap(const multimap<string, string> & m) const pair<multimap<string, string>::const_iterator, multimap<string, string>::const_iterator> mypair = m.equal_range("c++0x"); for(multimap<string, string>::const_iterator itr = mypair.first; itr!= mypair.second; ++itr) cout << itr->second << " has key C++0x" << endl;

Without C++0x: void PrintMultiMap(const multimap<string, string> & m) const pair<multimap<string, string>::const_iterator, multimap<string, string>::const_iterator> mypair = m.equal_range("c++0x"); for(multimap<string, string>::const_iterator itr = mypair.first; itr!= mypair.second; ++itr) cout << itr->second << " has key C++0x" << endl;

With C++0x: void PrintMultiMap(const multimap<string, string> & m) const auto mypair = m.equal_range("c++0x"); for(auto itr = mypair.first; itr!= mypair.second; ++itr) cout << itr->second << " has key C++0x" << endl;

With C++0x: void PrintMultiMap(const multimap<string, string> & m) const auto mypair = m.equal_range("c++0x"); for(auto itr = mypair.first; itr!= mypair.second; ++itr) cout << itr->second << " has key C++0x" << endl;

With C++0x: void PrintMultiMap(const multimap<string, string> & m) const auto mypair = m.equal_range("c++0x"); for(const auto& elem: mypair) cout << elem.second << " has key C++0x" << endl;

With C++0x: void PrintMultiMap(const multimap<string, string> & m) const auto mypair = m.equal_range("c++0x"); for(const auto& elem: mypair) cout << elem.second << " has key C++0x" << endl;

With C++0x: void PrintMultiMap(const multimap<string, string> & m) for(const auto& elem: m.equal_range("c++0x")) cout << elem.second << " has key C++0x" << endl;

With C++0x: void PrintMultiMap(const multimap<string, string> & m) for(const auto& elem: m.equal_range("c++0x")) cout << elem.second << " has key C++0x" << endl;

For Comparison: void PrintMultiMap(const multimap<string, string> & m) for(const auto& elem: m.equal_range("c++0x")) cout << elem.second << " has key C++0x" << endl; void PrintMultiMap(const multimap<string, string> & m) const pair<multimap<string, string>::const_iterator, multimap<string, string>::const_iterator> mypair = m.equal_range("c++0x"); for(multimap<string, string>::const_iterator itr = mypair.first; itr!= mypair.second; ++itr) cout << itr->second << " has key C++0x" << endl;

Type Inference The auto keyword tells the C++0x compiler to give the variable the same type as its initializer. Can be used for local variables only. Compiler must be able to figure out type; error otherwise.

Range-Based For Loop Syntax: for(type var : range)... Automatically creates an iterator and walks from begin() to end() Can traverse STL containers, pairs of iterators, arrays, strings, etc. Like the CS106B foreach loop, but much smarter.

Major Language Changes, pt. 2

Without C++0x: vector<string> ReadAllWords(const string& filename) ifstream input(filename.c_str()); vector<string> result; result.insert(result.begin(), istream_iterator<string>(input), istream_iterator<string>()); return result;

Without C++0x: vector<string> ReadAllWords(const string& filename) ifstream input(filename.c_str()); vector<string> result; result.insert(result.begin(), istream_iterator<string>(input), istream_iterator<string>()); return result; How efficient is this code?

result elems len 137 Alpha Beta Gamma... Chi Psi Omega

result Alpha return value Alpha elems Beta elems Beta Gamma Gamma len 137... len 137... Chi Chi Psi Psi Omega Omega

return value elems len 137 Alpha Beta Gamma... Chi Psi Omega

With C++0x: vector<string> ReadAllWords(const string& filename) ifstream input(filename); vector<string> result; result.insert(result.begin(), istream_iterator<string>(input), istream_iterator<string>()); return result;

With C++0x: vector<string> ReadAllWords(const string& filename) ifstream input(filename); vector<string> result; result.insert(result.begin(), istream_iterator<string>(input), istream_iterator<string>()); return result;

With C++0x: vector<string> ReadAllWords(const string& filename) ifstream input(filename); vector<string> result; result.insert(result.begin(), istream_iterator<string>(input), istream_iterator<string>()); return result; No major change to the code...

result elems len 137 Alpha Beta Gamma... Chi Psi Omega return value elems 137 len

result elems len 137 Alpha Beta Gamma... Chi Psi Omega return value elems 137 len

elems result nullptr len 0 Alpha Beta Gamma... Chi Psi Omega return value elems 137 len

elems result nullptr len 0 Alpha Beta Gamma... Chi Psi Omega return value elems 137 len

Move Semantics Copy Semantics (C++03): Can duplicate an object. Copy constructor and copy assignment operator Move Semantics (C++0x): Can move an object from one location to another Move constructor and move assignment operator Move semantics gives asymptotically better performance in many cases.

Rvalue Reference Syntax: Type && e.g. vector&&, string&&, int&& Reference to a temporary variable. Represents a variable whose contents can be moved from one location to another.

With C++0x: /* Move constructor */ template <typename T> vector<t>::vector(vector&& other) elems = other.elems; len = other.len; other.elems = nullptr; other.len = 0;

With C++0x: /* Move constructor */ template <typename T> vector<t>::vector(vector&& other) elems = other.elems; len = other.len; other.elems = nullptr; other.len = 0;

With C++0x: /* Move constructor */ template <typename T> vector<t>::vector(vector&& other) elems = other.elems; len = other.len; other.elems = nullptr; other.len = 0;

With C++0x: /* Move constructor */ template <typename T> vector<t>::vector(vector&& other) elems = other.elems; len = other.len; other.elems = nullptr; other.len = 0;

With C++0x: /* Move assignment operator. */ template <typename T> vector<t>& vector<t>::operator =(vector&& other) if(this!= &other) delete [] elems; elems = other.elems; len = other.len; other.elems = nullptr; other.len = 0; return *this;

With C++0x: /* Move assignment operator. */ template <typename T> vector<t>& vector<t>::operator =(vector&& other) if(this!= &other) delete [] elems; elems = other.elems; len = other.len; other.elems = nullptr; other.len = 0; return *this;

With C++0x: void MyClass::moveOther(MyClass&& other) /* Move all elements, then empty other. */ void MyClass::clear() /* Deallocate memory. */ MyClass::MyClass(MyClass&& other) moveother(move(other)); MyClass& MyClass::operator= (MyClass&& other) if(this!= &other) clear(); moveother(move(other)); return *this;

With C++0x: void MyClass::moveOther(MyClass&& other) /* Move all elements, then empty other. */ void MyClass::clear() /* Deallocate memory. */ MyClass::MyClass(MyClass&& other) moveother(move(other)); MyClass& MyClass::operator= (MyClass&& other) if(this!= &other) clear(); moveother(move(other)); return *this;

Move Semantics Copy Semantics Old-style copying behavior is still legal. Can still return objects by copying. C++0x tries move semantics first, falls back to copying. Objects can be movable without being copyable.

Without C++0x: void OpenFile(ofstream& toopen) while(true) cout << "Enter filename: "; toopen.open(getline().c_str()); if(!toopen.fail()) return; toopen.clear(); cout << "ohnoez no fielz!" << endl;

With C++0x: ofstream OpenFile() while(true) ofstream toopen; cout << "Enter filename: "; toopen.open(getline()); if(!toopen.fail()) return toopen; cout << "ohnoez no fielz!" << endl;

Summary of Move Semantics Makes existing code more efficient. Makes future code easier to understand. Primarily for library designers, but extremely useful nonetheless.

Major Language Changes, pt. 3

pair make_pair Without C++0x:

With C++0x: pair make_pair tuple make_tuple tuple_cat tie

Without C++0x: bind1st bind2nd ptr_fun mem_fun mem_fun_ref not1 not2

With C++0x: bind1st bind2nd bind ptr_fun function mem_fun mem_fn mem_fun_ref not1 not2

Without C++0x: vector deque list set multiset stack queue priority_queue map multimap bitset

With C++0x: vector deque list forward_list array stack queue priority_queue bitset set multiset unordered_set unordered_multiset map multimap unordered_map unordered_multimap

Without C++0x: accumulate adjacent_difference adjacent_find binary_search copy copy_backward count count_if equal equal_range fill fill_n find find_end find_first_of find_if for_each generate generate_n includes inner_product inplace_merge iter_swap lexicographical_compare lower_bound make_heap max max_element merge min min_element mismatch next_permutation nth_element partial_sort partial_sort_copy partial_sum partition pop_heap prev_permutation push_heap random_shuffle remove remove_copy remove_copy_if remove_if replace replace_copy replace_copy_if replace_if reverse reverse_copy rotate rotate_copy search search_n set_difference set_intersection set_symmetric_difference set_union sort sort_heap stable_partition upper_bound stable_sort swap swap_ranges transform unique unique_copy

With C++0x: accumulate adjacent_difference adjacent_find binary_search copy copy_backward count count_if equal equal_range fill fill_n find find_end find_first_of find_if for_each generate generate_n includes inner_product inplace_merge iter_swap lexicographical_compare lower_bound make_heap max max_element merge min min_element mismatch next_permutation nth_element partial_sort partial_sort_copy partial_sum partition pop_heap prev_permutation push_heap random_shuffle remove remove_copy remove_copy_if remove_if replace replace_copy replace_copy_if replace_if reverse reverse_copy rotate rotate_copy search search_n set_difference set_intersection set_symmetric_difference set_union sort sort_heap stable_partition upper_bound stable_sort swap swap_ranges transform unique unique_copy all_of any_of none_of find_if_not copy_n copy_if move move_backward is_partitioned partition_copy partition_point is_sorted is_heap is_heap_until minmax minmax_element iota

Supercharging namespace std Major extensions to existing libraries STL, streams, numerics, etc. Increases overall complexity of language. Makes language easier to use.

And a few more things...

Multithreading

Multithreading vector<string> ReadFile(string filename); void LoadAllData(vector<string>& longlatdata, vector<string>& geocodes); auto llreader = async(readfile, "long-lat.txt"); auto georeader = async(readfile, "geo-codes.txt"); longlatdata = llreader.get(); georeader = georeader.get();

Lambda Expressions

Lambda Expressions /* Prints out all long words. */ copy_if(v.begin(), v.end(), ostream_iterator<string>(cout, "\n"), [](string str) -> bool return str.size() > 6 );

Other C++0x Features Variadic Templates Smart Pointers Random Number Generators Regular Expression Matchers Compile-Time Arithmetic Generalized Constant Expressions Plus a whole lot more.

Summary of C++0x C++0x will make programmers more efficient. Type inference and ranged-based for loops will greatly enhance programmer productivity. Enhanced STL makes it easier to write good code. Move semantics make functions clearer. C++0x will make good libraries easy to write. Move semantics allow for very elaborate library design. Synergy!