Download presentation
Presentation is loading. Please wait.
Published byἈριστοφάνης Γαλάνη Modified over 6 years ago
1
A brief look at some of the new features introduced to the language
C++ ’11 and C++ ‘14 A brief look at some of the new features introduced to the language Copyright © by Curt Hill
2
Copyright © 2012-2017 by Curt Hill
History Although the language was developed at Bell Labs, it has periodically gone through the standardization process There was one in 1985, 1998, 2003 and the most recent was in 2011 Bjarne Stroustrup has led the team in 1979 at Bell Labs and has always been seriously involved The standard has sometimes been referred to c++’0x They anticipated earlier finalization Copyright © by Curt Hill
3
Standards and Compilers
Some compilers take more pride in adhering to the standard Typically the open source come the closest, but slowest GCC Commercial compilers want to be seen as modern so tend to comply However, they want to be non-standard to keep their users from migrating Some features have already been implemented some are waiting Copyright © by Curt Hill
4
Copyright © 2012-2017 by Curt Hill
Lambda Expressions A mathematical expression devised in 20th century These have been in LISP since 1959 In Java as well A lambda expression is a function that is defined right where needed Instead of defined before and called Also known as anonymous functions Copyright © by Curt Hill
5
Functions as Parameters
For some time C++ has supported function objects Really just a pointer at a function Recall the linked list example We would like to apply a function to each item in the list There are two ways: Iterators Functions as parameter Copyright © by Curt Hill
6
Copyright © 2012-2017 by Curt Hill
Example class LinkedNode{ private: friend LinkedList; float data; LinkedNode * next; LinkedNode(); }; class LinkedList{ friend class LinkedIterator; LinkedNode * root public: void touchAll(void (*fn)(float)); Copyright © by Curt Hill
7
Copyright © 2012-2017 by Curt Hill
What was that? Did you notice: void touchAll(void (*fn)(float)); It is a function that takes a function as a parameter A typical definition follows Copyright © by Curt Hill
8
Copyright © 2012-2017 by Curt Hill
Example void LinkedList::touchAll( void (*fn)(float)){ LinkedNode * ptr = root; while(ptr != null){ (*fn)(ptr->data); ptr = ptr -> next; } Copyright © by Curt Hill
9
Copyright © 2012-2017 by Curt Hill
Continuing I may now pass any function that has the correct signature to touchAll I mention the function, but I give no parameters to it Consider the next screen Copyright © by Curt Hill
10
Copyright © 2012-2017 by Curt Hill
Example void sum(float f){…} void max(float f){…} void min(float f){…} LinkedList myList; … myList.touchAll(sum); Mylist.touchAll(max); Mylist.touchAll(min); Copyright © by Curt Hill
11
Copyright © 2012-2017 by Curt Hill
Commentary Any function that takes a float and has return type void may be passed to touchAll The method touchAll will walk through the list At each node it will call the passed function Without ever knowing what the function actually was or did That is a function as a parameter or a function object Copyright © by Curt Hill
12
Copyright © 2012-2017 by Curt Hill
Lambdas The form is: [capture](params)->return-type {body} The capture part is in square brackets and describes the local variables that may be used These may be by value or reference Params are like any parameter Return type is optional if all the returns agree in type The body looks like any function’s body Copyright © by Curt Hill
13
Copyright © 2012-2017 by Curt Hill
Example A simple example: [](int x, int y) { return x + y; } This could only be used where a two integer function could be used Recall the touchAll method Now we no longer need a defined function we can just make up one as we go along Copyright © by Curt Hill
14
Copyright © 2012-2017 by Curt Hill
Example void doIt(LinkedList & mL){ float sum = 0; … ml.touchAll([&sum](float f){ sum += f; } ); Copyright © by Curt Hill
15
Copyright © 2012-2017 by Curt Hill
What was that? The touchAll method expects a function It gets one made up on the spot The capture list allowed the anonymous function to access sum by reference This saved the sum in a local variable of the function doIt There are many times we can pass functions, now they do not have to already defined Copyright © by Curt Hill
16
Generalized Constants
Recall that an array must have a constant size Provided it is not dynamic Thus the following is not legal int get_five() {return 5;} int array [get_five()+7]; Whereas: int array [5+7]; Is legal Copyright © by Curt Hill
17
Copyright © 2012-2017 by Curt Hill
Commentary It is clear to us that this should be OK, but the compiler is not allowed to peek inside the get_five and determine that it has no run-time effects One of the problems is that when the compiler looks at get_five it does not know what it will be used for Thus it cannot allow this The 2011 standard introduces a constexpr prefix and keyword that allows this Copyright © by Curt Hill
18
Copyright © 2012-2017 by Curt Hill
New Array Example Now we can say: constexpr int get_five() {return 5;} int array [get_five()+7]; This is legal If a function with the constexpr prefix does anything that may only be done at runtime a syntax error is issued Similar to an attempt to change a constant reference parameter Copyright © by Curt Hill
19
Copyright © 2012-2017 by Curt Hill
Type Inference In previous versions every data value must have an explicitly declared type Variables, constants, parameters, function results The language will now let the type be left out if it may be inferred This usually requires the auto keyword auto myVar = 10; The integer type may be inferred Copyright © by Curt Hill
20
Copyright © 2012-2017 by Curt Hill
Expansion In C this is expanded The auto may be the return type in a function However you must be consistent: auto fn(int a){ if(a>5) return 0; return 5.5; } This is not legal since two types are returned Copyright © by Curt Hill
21
Copyright © 2012-2017 by Curt Hill
Another Also in 2014 a lambda function may have an auto parameter This makes it basically a generic Consider: auto adder = [] (auto op1, auto op2){ return op1+op2; } This can be used multiple times with different parameter types Copyright © by Curt Hill
22
Copyright © 2012-2017 by Curt Hill
Templates Recall the problems with templatizing the set program? In the client specific types had to be used The template class needed no modification, but the client did With this we can generalize the client in the same way that we generalized the list Copyright © by Curt Hill
23
Copyright © 2012-2017 by Curt Hill
Range Based For Often we use a for to process a range that we could infer Similar to what is done in Java Consider: int arr[10]; … for(int i = 0;i<10;i++) There is now a for that will infer the range: for(int &x: arr) x++; Copyright © by Curt Hill
24
Copyright © 2012-2017 by Curt Hill
For Usage Similar to a construct in Java Using: for(int x: ob) It will assign to x each successive element of ob If x is preceded by & the element may be changed Otherwise it is a read only access This may be used in any way the subscripted variable may be Copyright © by Curt Hill
25
Copyright © 2012-2017 by Curt Hill
Range For Data It may be used for: Arrays whose declaration may be seen Not those passed as parameters Initializer lists Enumerations for(int x:{2,4,9,16}) … Any object that has a begin() and end() method Such as STL iterators Copyright © by Curt Hill
26
Copyright © 2012-2017 by Curt Hill
Deprecated The 2014 standard also adds a deprecated attribute Implementation in a compiler is optional but a warning on using it is generally advisable Copyright © by Curt Hill
27
Copyright © 2012-2017 by Curt Hill
Conclusion There are several other things too numerous to mention here These are available in several compilers The 2017 standard will soon make changes Copyright © by Curt Hill
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.