Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

C++ Dev Bootcamp 2014 Day 4 (C++11 and STL)

Gunjan Kumar | Computer Scientist

2014 Adobe Systems Incorporated.


Confidential.

All Rights Reserved.

Adobe

Agenda C++ Dev Bootcamp Day 4

History, Why C++11

C++11 New Keywords

auto, nullptr

rValue references, move constructor, std::move

decltype, static_assert

STL Containers, Iterators

Sequence Container

Associative Container

string

lambda expression

Smart Pointers (shared, weak and unique pointer)

STL Algorithms

sort, find, binary_search

for_each

References

2014 Adobe Systems Incorporated.


Confidential.

All Rights Reserved.

Adobe

C++11 Introduction
C++ Dev Bootcamp 2014 Day 4

2014 Adobe Systems Incorporated.


Confidential.

All Rights Reserved.

Adobe

History of C++11

In 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++98

In 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++11

In 2014, C++14 is a small extension over C++11

In 2017, new C++ standard is planned.

2014 Adobe Systems Incorporated.


Confidential.

All Rights Reserved.

Adobe

C++ Basics (Copied from Bjarne Stroustrup slide)

2014 Adobe Systems Incorporated.


Confidential.

All Rights Reserved.

Adobe

Why C++11

C++11 makes code dramatically easier, cleaner to write, and


faster.

No raw pointer operation that means no crash or memory leak.

2014 Adobe Systems Incorporated.


Confidential.

All Rights Reserved.

Adobe

C++11 New Keywords


C++ Dev Bootcamp 2014 Day 4

2014 Adobe Systems Incorporated.


Confidential.

All Rights Reserved.

Adobe

Auto

The 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 syntax

2014 Adobe Systems Incorporated.


Confidential.

All Rights Reserved.

Adobe

Auto

It makes typing easy and improves readability.

C++03

C++11

map<string,string>::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.


Confidential.

All Rights Reserved.

Adobe

nullptr

Nullptr 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

2014 Adobe Systems Incorporated.


Confidential.

All Rights Reserved.

Adobe

10

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; // Error

C++11 adds a new non-const reference type called an rvalue


reference, identified by T&&
Example:
int&& rI = 5; //new rValue
rI = 10; //OK

Another 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
2014 Adobe Systems Incorporated.
Confidential.

All Rights Reserved.

Adobe

11

Move Constructor

New 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;
};

2014 Adobe Systems Incorporated.


Confidential.

All Rights Reserved.

Adobe

12

Move constructor

Std::move is used to call move constructor explicitly.


MyString movedText = std::move(strText);

2014 Adobe Systems Incorporated.


Confidential.

All Rights Reserved.

Adobe

13

decltype

decltype 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&

2014 Adobe Systems Incorporated.


Confidential.

All Rights Reserved.

Adobe

14

decltype

More 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 rvalue
const int&& foo();
decltype(foo()) x1 = i; // type is const int&&
auto multiply (int x, int y) -> decltype(x * y);
template <typename T1, typename T2> // generic return type
auto add(T1 a, T2 b) -> decltype(a + b) {
return a + b;
}

2014 Adobe Systems Incorporated.


Confidential.

All Rights Reserved.

Adobe

15

static_assert

Any 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 <typename T>
void print(T a) {
static_assert(sizeof(T) == 4, "Unsupported Type, size should be 4");
cout << a << endl;
cout << b << endl;
}
void TestStaticAssert() {
print(1);
print("test");
print('a'); // "Unsupported Type, size should be 4"
}

2014 Adobe Systems Incorporated.


Confidential.

All Rights Reserved.

Adobe

16

2014 Adobe Systems Incorporated.


Confidential.

All Rights Reserved.

Adobe

You might also like