ncg training day 4 part-1 (c++11 introduction and new keywords).pptx

Upload: abhishek-jindal

Post on 13-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Adobe Title

Gunjan Kumar | Computer ScientistC++ Dev Bootcamp 2014 Day 4 (C++11 and STL) 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Agenda C++ Dev Bootcamp Day 4History, Why C++11C++11 New Keywordsauto, nullptrrValue references, move constructor, std::movedecltype, static_assertSTL Containers, IteratorsSequence ContainerAssociative Containerstringlambda expressionSmart Pointers (shared, weak and unique pointer)STL Algorithmssort, find, binary_searchfor_eachReferences

2 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

C++ Dev Bootcamp 2014 Day 4C++11 Introduction 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.History of C++11In 1979 Bjarne Stroustrup was doing work for his Ph.D. thesis, he got opportunity to work on Simula. He liked the language and shortly after he started to work on C with Classes.In 1983, C with Classes is renamed to C++In 1998, C++ standard committee published first international standard for C++. This was informally knows as C++98In 2003, C++03 reviewed the C++98 standard from programmer points of view.In 2005, the C++ standards committee released a technical report (dubbed TR1) detailing various features they were planning to add to the latest C++ standard. The new standard was informally dubbed C++0x.In 2011, new C++ standard was finished dubbed C++11In 2014, C++14 is a small extension over C++11In 2017, new C++ standard is planned.

4 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.C++ Basics (Copied from Bjarne Stroustrup slide)5

2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Why C++11C++11 makes code dramatically easier, cleaner to write, and faster.No raw pointer operation that means no crash or memory leak.6 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

C++ Dev Bootcamp 2014 Day 4C++11 New Keywords 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.AutoThe auto specifier is a placeholder for a type to be deduced The auto type-specifier signifies that the type of a variable being declared shall be deduced from its initializer or that a function declarator shall include a trailing-return-type.Example:auto x = 5; // OK: x has type int const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int static auto y = 0.0; // OK: y has type double auto int r; // error: auto is not a storage-class-specifier auto pX = new auto(1); // allocated type is int auto x = new auto(a); // allocated type is char, x is of type char*auto multiply (int x, int y) -> int; // new function syntax8 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.AutoIt makes typing easy and improves readability.

9C++03C++11map::iterator it = m.begin(); double const param = config["param"]; singleton& s = singleton::instance();

auto it = m.begin(); auto const param = config["param"]; auto& s = singleton::instance();

2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.nullptrNullptr is new keyword to serve as a distinguished null pointer constant: nullptr. It is of type nullptr_t, which is implicitly convertible and comparable to any pointer type or pointer-to-member type. It is not implicitly convertible or comparable to integral types, except for bool to support backward compatibility. Example: int * pX = nullptr;int x; pX = &x; if(pX==nullptr) //OK if(x == nullptr) //Error

10 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.rValue references Earlier rvalue are lavalue are distinguished by right side or left side of expression. Example: const int& i = 5; //old rvalue i = 10; // ErrorC++11 adds a new non-const reference type called an rvalue reference, identified by T&& Example: int&& rI = 5; //new rValue rI = 10; //OKAnother way to think:- If you are able to take address of expression it is lvalue otherwise it is rvalue. Example: int i = 10; // i is lvalue, 10 is rvalue string name = fname + rname; // name is lvalue and (fname+rname) is rvalue // as &name is valid but &(fname+rname) is not valid

11 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Move ConstructorNew rValue reference gives power to write a move constructor and a move assignment operator for a C++ class.A move constructor enables you to implement move semantics, which can significantly improve the performance of your applications.Example:class MyString { public: MyString(MyString& a_MyString) : m_pData(nullptr) { if(a_MyString.m_pData) m_pData = new std::string(*a_MyString.m_pData); //Creating new memory and then copy }MyString(MyString&& a_MyString) { m_pData = a_MyString.m_pData; //no new memory or copy, simply move a_MyString.m_pData=nullptr; }private: std::string* m_pData;};12 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Move constructorStd::move is used to call move constructor explicitly. MyString movedText = std::move(strText);13 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.decltypedecltype is an operator for querying the type of an expression. The keyword decltype can be used to determine the type of an expression at compile-time. Example:int i; struct A { double x; }; const A* a = new A(); decltype(i) x2; // type is int decltype(a->x) x3; // type is double decltype((a->x)) x4 = x3; // type is const double&

14 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.decltypeMore example:auto c = 0; // c has type int auto d = c; // d has type int decltype(c) e; // e has type int, the type of the entity named by c decltype((c)) f = c; // f has type int&, because (c) is an lvalue decltype(0) g; // g has type int, because 0 is an rvalueconst int&& foo(); decltype(foo()) x1 = i; // type is const int&&

auto multiply (int x, int y) -> decltype(x * y); template // generic return typeauto add(T1 a, T2 b) -> decltype(a + b) { return a + b;}

15 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.static_assertAny compile time operator or const_expr function variables can be used for static_ assert. Now we can write better test case to validate at compile time.Example:template void print(T a) { static_assert(sizeof(T) == 4, "Unsupported Type, size should be 4"); cout