Other C++11/14 features

Similar documents
Templates. Zoltán Porkoláb: C++11/14 1

1 enum class -- scoped and strongly typed enums

Mixin and CRTP in C++98/11. Zoltán Porkoláb, PhD.

Basic Types, Variables, Literals, Constants

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

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

Remedial C Now that we can actually use it Pete Williamson

Tokens, Expressions and Control Structures

C The new standard

GridKa School 2013: Effective Analysis C++ Templates

Introduction to Move Semantics in C++ C and C

Proposed Wording for Variadic Templates (Revision 1)

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

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

Variables. Data Types.

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

Object-Oriented Principles and Practice / C++

Modern and Lucid C++ Advanced for Professional Programmers. Part 7 Compile-Time Computation. Department I - C Plus Plus

Proposed Wording for Variadic Templates

CS

Improving Enumeration Types [N1513= ] David E. Miller

An Oddysey in C++ A Guided Tour of Modern C++ Per Karlström Upplysningen

Assignment #1 Advanced Programming in C /2018. NPRG051 Advanced programming in C++ - Assignment #1

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

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

15. Pointers, Algorithms, Iterators and Containers II

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

C++11 and Compiler Update

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

use static size for this buffer

New Tools for Class and Library Authors

Introduction to C++11 and its use inside Qt

IBM Rational Rhapsody TestConductor Add On. Code Coverage Limitations

Working with Batches of Data

Advanced C ++ Philip Blakely. Laboratory for Scientific Computing, University of Cambridge. Philip Blakely (LSC) Advanced C++ 1 / 119

Using Enum Structs as Bitfields

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

TDDD38 - Advanced programming in C++

Operator Dot Wording

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

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

Before we dive in. Preprocessing Compilation Linkage

Programming Language C++11. Summary of changes and general feature description. Niedl, Markus

C++ Template Meta. A basic introduction to basic C++ techniques used in template metaprogramming.

Cpt S 122 Data Structures. Templates

How the Adapters and Binders Work

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

Elevate your Code to Modern C++ with Automated Tooling. Peter Sommerlad

PENN STATE UNIVERSITY Department of Economics

Fundamentals of Type-Dependent Code Reuse in C++ Mark Isaacson

The C++ Programming Language, Core Working Group. Title: Unary Folds and Empty Parameter Packs (revision 1)

Introduction to C++ Systems Programming

std::optional from Scratch

Part II. C continued. Philip Blakely (LSC) Advanced C++ 35 / 119

General Advise: Don t reinvent the wheel

C++11 The Future is here

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

From Wikipedia, the free encyclopedia

An Introduction to Template Metaprogramming

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

GCC : From 2.95 to 3.2

14. Pointers, Algorithms, Iterators and Containers II

Contract Programming For C++0x

C++ Programming Lecture 7 Software Engineering Group

Expansion statements. Version history. Introduction. Basic usage

COMP6771 Advanced C++ Programming

The Standard Template Library

SFU CMPT Topic: Class Templates

P1267R0: Custom Constraint Diagnostics

Wording for lambdas in unevaluated contexts

Part X. Advanced C ++

Debug C++ Without Running. Anastasia Kazakova

Program template-smart-pointers-again.cc

C++ Modern and Lucid C++ for Professional Programmers

QStringView. everywhere. Marc Mutz, Senior Software Engineer at KDAB

Class Types in Non-Type Template Parameters

Constants, References

19. Dynamic Data Structures II

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

Programming at Compile Time. Rainer Grimm Training, Coaching, and Technology Consulting

COMP6771 Advanced C++ Programming

Implicit Evaluation of auto Variables and Arguments

Rvalue References, Move Semantics, Universal References

Introduction to Core C++

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

Due Date: See Blackboard

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

We look back... #include <iostream> #include <vector>

Short Notes of CS201

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

Managing Memory. (and low level Data Structures) Lectures 22, 23. Hartmut Kaiser.

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

IBM i Version 7.3. Programming IBM Rational Development Studio for i ILE C/C++ Language Reference IBM SC

On the correctness of template metaprograms

C-types: basic & constructed. C basic types: int, char, float, C constructed types: pointer, array, struct

C++ Programming Lecture 7 Software Engineering Group

CS201 - Introduction to Programming Glossary By

Working Draft, Extensions to C++ for Modules

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

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Transcription:

Other C++11/14 features Auto, decltype Range for Constexpr Enum class Initializer list Default and delete functions Etc. Zoltán Porkoláb: C++11/14 1

Auto, decltype template<class T> void printall(const vector<t>& v) for (typename vector<t>::const_iterator p = v.begin(); p!=v.end(); ++p) cout << *p << "\n"; // C++11 template<class T> void printall(const vector<t>& v) for (auto p = v.begin(); p!=v.end(); ++p) cout << *p << "\n"; void f(const vector<int>& a, vector<float>& b) typedef decltype(a[0]*b[0]) Tmp; for (int i=0; i<b.size(); ++i) Tmp* p = new Tmp(a[i]*b[i]); //... Zoltán Porkoláb: C++11/14 2

Example template <class T, class S>? max( T a, S b) // How to define the return value? if ( a > b ) return a; else return b; int main() short is = 3; long il = 2; double d = 3.14; cout << max( il, is); // long is ''better'' than short cout << max( is, d); // double is ''better'' than short Zoltán Porkoláb: C++11/14 3

Example template <class T, class S> auto max( T a, S b) -> decltype(a+b) if ( a > b ) return a; else return b; int main() short is = 3; long il = 2; double d = 3.14; cout << max( il, is); // long cout << max( is, d); // double Zoltán Porkoláb: C++11/14 4

Example template <class T, class S> typename std::common_type<t,s>::value max( T a, S b) if ( a > b ) return a; else return b; int main() short is = 3; long il = 2; double d = 3.14; cout << max( il, is); // long cout << max( is, d); // double Zoltán Porkoláb: C++11/14 5

Range for void f(const vector<double>& v) for (auto x : v) cout << x << '\n'; for (auto& x : v) ++x; // using reference allows us to change value // You can read that as "for all x in v" going through starting with // v.begin() and iterating to v.end(). Another example: for (const auto x : 1,2,3,5,8,13,21,34 ) cout << x << '\n'; // The begin() (and end()) can be a member to be called v.begin() // or a free-standing function to be called begin(v). Zoltán Porkoláb: C++11/14 6

Enum classes enum Alert green, yellow, election, red ; // traditional enum enum class Color red, blue ; // scoped and strongly typed enum // no export of enumerator names into enclosing scope // no implicit conversion to int enum class TrafficLight red, yellow, green ; Alert a = 7; Color c = 7; int a2 = red; int a3 = Alert::red; int a4 = blue; int a5 = Color::blue; Color a6 = Color::blue; // error (as ever in C++) // error: no int->color conversion // ok: Alert->int conversion // error in C++98; ok in C++0x // error: blue not in scope // error: not Color->int conversion // ok enum class Color : char red, blue ; // compact representation enum class TrafficLight red, yellow, green ; // by default, the underlying type is int enum E E1 = 1, E2 = 2, Ebig = 0xFFFFFFF0U ; // how big is an E? enum class EE : unsigned long EE1 = 1, EE2 = 2, EEbig = 0xFFFFFFF0U ; // we are specific enum class Color_code : char; void foobar(color_code* p); // (forward) declaration // use of forward declaration enum class Color_code : char red, yellow, green, blue ; // definition Zoltán Porkoláb: C++11/14 7

Initializer list vector<double> v = 1, 2, 3.456, 99.99 ; list<pair<string,string>> languages = "Nygaard","Simula", "Richards","BCPL", "Ritchie","C" ; map<vector<string>,vector<int>> years = "Maurice","Vincent","Wilkes",1913, 1945, 1951, 1967, 2000, "Martin","Ritchards", 1982, 2003, 2007, "David","John","Wheeler", 1927, 1947, 1951, 2004 ; BUT! auto x1 = 5; // deduced type is int auto x2(5); // deduced type is int auto x3 5 ; // deduced type is std::initializer_list (will fixed) auto x4 = 5 ;// deduced type is std::initializer_list Zoltán Porkoláb: C++11/14 8

Default and delete functions class X //... X& operator=(const X&) = delete; X(const X&) = delete; ; // disallow copying // Conversely, we can also say explicitly that // we want to default copy behavior: class Y //... Y& operator=(const Y&&) = default; Y(const Y&&) = default; ; // default move semantics Zoltán Porkoláb: C++11/14 9

Delegated constructors // C++98 class X int a; validate(int x) if (0<x && x<=max) a=x; else throw bad_x(x); public: X(int x) validate(x); X() validate(42); X(string s) int x = lexical_cast<int>(s); validate(x); //... ; // C++11 class X int a; public: X(int x) if (0<x && x<=max) a=x; else throw bad_x(x); X() : X42 X(string s) :Xlexical_cast<int>(s) //... ; Zoltán Porkoláb: C++11/14 10

Use constructors // C++11 class Derived : public Base public: using Base::f; // lift Base's f into Derived's scope -- works in C++98 void f(char); // provide a new f void f(int); // prefer this f to Base::f(int) ; using Base::Base; // lift Base constructors Derived's scope C++11 only Derived(char); // provide a new constructor Derived(int); // prefer this constructor to Base::Base(int) //... Zoltán Porkoláb: C++11/14 11

Using (C++11) Typedef won't work well with templates Using introduce type alias using myint = int; template <class T> using ptr_t = T*; void f(int) // void f(myint) syntax error: redeclaration of f(int) // make mystring one parameter template template <class CharT> using mystring = std::basic_string<chart,std::char_traits<chart>>; Zoltán Porkoláb: C++11/14 12

Variadic templates (C++11) Type pack defines sequence of type parameters Recursive processing of pack template<typename T> T sum(t v) return v; template<typename T, typename... Args> // template parameter pack T sum(t first, Args... args) // function parameter pack return first + sum(args...); int main() double lsum = sum(1, 2, 3.14, 8L, 7); std::string s1 = "x", s2 = "aa", s3 = "bb", s4 = "yy"; std::string ssum = sum(s1, s2, s3, s4); Zoltán Porkoláb: C++11/14 13

Mixin reloded (C++11) Variadic templates make us possible to define variadic set of base struct A ; struct B ; struct C ; struct D ; template<class... Mixins> class X : public Mixins... public: X(const Mixins&... mixins) : Mixins(mixins)... ; int main() A a; B b; C c; D d; X<A,B,C,D> xx(a,b,c,d); Zoltán Porkoláb: C++11/14 14

Vector size and capacity int main() std::vector<int> v; std::cout << "Default-constructed capacity is " << v.capacity() << '\n'; v.resize(100); std::cout << "Capacity of a 100-element vector is " << v.capacity() << '\n'; v.clear(); std::cout << "Capacity after clear() is " << v.capacity() << '\n'; // std::vector<int>(v).swap(v); // C++98 v.shrink_to_fit(); // C++11 std::cout << "Capacity after shrink_to_fit() is " << v.capacity() << '\n'; Default-constructed capacity is 0 Capacity of a 100-element vector is 100 Capacity after clear() is 100 Capacity after shrink_to_fit() is 0 Zoltán Porkoláb: C++11/14 15

Others // user defined literals. Identifiers not starting with underscore are reserved for std:: constexpr complex<double> operator "" i(long double d) // imaginary literal return 0,d; // complex is a literal type // nullptr char* p = nullptr; void f(int); void f(char*); f(0); f(nullptr); // call f(int) // call f(char*) // static assert int f(int* p, int n) // ok, compile time static_assert( sizeof(*p) < 0,"*p is too big"); // error: static_assert() expression not a constant expression static_assert( p!=0,"p is null"); //... Zoltán Porkoláb: C++11/14 16