C 11 and C 14 A brief look

C++ ’ 11 and C++ ‘ 14 A brief look at some of the new features introduced to the language 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++’ 0 x – They anticipated earlier finalization Copyright © 2012 -2017 by Curt Hill

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 © 2012 -2017 by Curt Hill

Lambda Expressions • A mathematical expression devised in 20 th 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 © 2012 -2017 by Curt Hill

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 © 2012 -2017 by Curt Hill

Example class Linked. Node{ private: friend Linked. List; float data; Linked. Node * next; Linked. Node(); }; class Linked. List{ friend class Linked. Iterator; Linked. Node * root public: void touch. All(void (*fn)(float)); Copyright © 2012 -2017 by Curt Hill

What was that? • Did you notice: void touch. All(void (*fn)(float)); • It is a function that takes a function as a parameter • A typical definition follows Copyright © 2012 -2017 by Curt Hill

Example void Linked. List: : touch. All( void (*fn)(float)){ Linked. Node * ptr = root; while(ptr != null){ (*fn)(ptr->data); ptr = ptr -> next; } } Copyright © 2012 -2017 by Curt Hill

Continuing • I may now pass any function that has the correct signature to touch. All • I mention the function, but I give no parameters to it • Consider the next screen Copyright © 2012 -2017 by Curt Hill

Example void sum(float f){…} void max(float f){…} void min(float f){…} Linked. List my. List; … my. List. touch. All(sum); … Mylist. touch. All(max); … Mylist. touch. All(min); Copyright © 2012 -2017 by Curt Hill

Commentary • Any function that takes a float and has return type void may be passed to touch. All • The method touch. All 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 © 2012 -2017 by Curt Hill
->return-type {body} • The capture part is in • The form is: Lambdas [capture](params)->return-type {body} • The capture part is in](http://slidetodoc.com/presentation_image_h2/5cfdc751ad06330e27fb5c7520ca4f1e/image-12.jpg)
• The form is: Lambdas [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 © 2012 -2017 by Curt Hill
 { return x + y; Example • A simple example: [](int x, int y) { return x + y;](http://slidetodoc.com/presentation_image_h2/5cfdc751ad06330e27fb5c7520ca4f1e/image-13.jpg)
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 touch. All method • Now we no longer need a defined function we can just make up one as we go along Copyright © 2012 -2017 by Curt Hill

Example void do. It(Linked. List & m. L){ float sum = 0; … ml. touch. All([&sum](float f){ sum += f; } ); Copyright © 2012 -2017 by Curt Hill

What was that? • The touch. All 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 do. It • There are many times we can pass functions, now they do not have to already defined Copyright © 2012 -2017 by Curt Hill

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 © 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 © 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 © 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 my. Var = 10; • The integer type may be inferred Copyright © 2012 -2017 by Curt Hill

Expansion • In C++ 2014 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 © 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 op 1, auto op 2){ return op 1+op 2; } • This can be used multiple times with different parameter types 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 © 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 © 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 © 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 © 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 © 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 © 2012 -2017 by Curt Hill
- Slides: 27