C++1* Tech Talks

C++1* Tech Talks Initialization, Construction and Deconstruction

Hannes Hauswedell and Wikipedia, and Stackoverflow...

September 16, 2016

1 / 12

C++1* Tech Talks

Initialization

Default initialization Value initialization Direct initialization Copy initialization Direct List initialization Copy List initialization Aggregate initialization Reference initialization

std::string s; std::string s{}; std::string s{"Foo"}; std::string s = "Foo"; std::string s{’F’,’o’,’o’}; std::string s = {’F’,’o’,’o’}; char a[3] = {’a’, ’b’}; char& c = a[0];

std::string s(); std::string s("Foo"); std::string s(s2);

T t{’F’,4};

2 / 12

C++1* Tech Talks

Default Initialization – no () or {}

2

std :: string s ; std :: string * p_s = new std :: string ; // -> default constructed

4 6

std :: string sa [5]; std :: string p_sa = new std :: string [5]; // -> elements are default initialized which means default constructed

8 10

int i ; int * p_i = new int ; // -> not initialized at all

12 14

int ia [5]; int p_ia = new int [5]; // -> elements are default initialized which means not initialized

Summary: default constructor or no initialization

3 / 12

C++1* Tech Talks

Value Initialization – empty () or {} 1 3 5

std :: string s () ; std :: string s {}; std :: string * p_s = new std :: string () ; std :: string * p_s = new std :: string {}; // -> std :: string has user - provided default constr . // = > default initialized which means default constructed

7 9 11

int i () ; int i {}; int * p_i = new int () ; int * p_i = new int {}; // POD , so zero - initialized , which means == 0

13 15 17

T t {}; // type with implicit default constructor T t () ; // ... // first zero - initialized , then default initialized ( dunno why )

Summary: zero initializes PODs and POD members of aggregate types; default initializes the rest 4 / 12

C++1* Tech Talks

Direct Initialization – () or {} with arguments or casts 1 3 5 7 9 11

std :: string s ( " Foo " ) ; std :: string s { " Foo " }; std :: string * p_s = new std :: string ( " Foo " ) ; std :: string * p_s = new std :: string { " Foo " }; // -> best matching constructor is selected for in itializ ation int i (0) ; int i {7.3}; int * p_i = new int ( -5.3) ; int * p_i = new int {7}; // non - class types try conversions from the argument to the type

Summary: I

“regular” constructor selection

I

does not apply to arrays

I

narrowing conversions allowed 5 / 12

C++1* Tech Talks

Copy Initialization – using the assignment operator

1

std :: string s = " Foo " ; // -> best matching constructor is selected for in itializ ation

3 5

int i = 0; int i = 7.3; // non - class types try conversions from the argument to the type

Summary: I

only non-explicit constructors used

I

otherwise same as direct initialization, but with =

I

avoid this except for PODs, use Direct Initialization instead

6 / 12

C++1* Tech Talks

Aggregate initialization 2 4 6 8 10 12

struct S { int x ; struct Foo { int i ; int j ; int a [3]; } b; }; S S S S

s1 = {1 , {2 , 3 , {4 , 5 , s2 = {1 , 2 , 3 , 4 , 5 , s3 {1 , {2 , 3 , {4 , 5 , s4 {1 , 2 , 3 , 4 , 5 ,

6} } }; 6 }; 6} } }; 6 };

// // // //

copy - initializes elements from args with brace elision using direct - list - ini tializat ion syntax error in C ++11 , but okay since C ++14

14 16

S s5 S s6

{1 , {2 , 3 , {4 , 5 } } }; // last value in b . a will be value / zero - initialized {1 , {2 , 3 , {4 , 5 , 6 , 7 } } }; // compile - time error , ill - formed

18

int ai [] = {1 , 2.0 };

// narrowing conversion : okay in C ++03 , error since C ++11

Summary: I makes many constructors redundant, simplifies code I prefer the syntax without assignment operator and avoid brace elision 7 / 12

C++1* Tech Talks

List initialization / uniform initialization syntax – everything with {} 2

T object { arg1 , arg2 , ... }; T object = new T { arg1 , arg2 , ... }; T object = { arg1 , arg2 , ... };

// direct list init ializati on // direct list initiali zation // copy list in itializa tion

Summary: I

generalized uniform syntax by using braces with 0-n args { arg1, arg3, ... }

I

zero args and non-aggregate type → value initialization

I

one arg and has same type as T → direct / copy initialization

I

T is an aggregate Type → aggregate initialization one arg and different type or more args and T is non-aggregate class type:

I

I I

look for constructors of T with std::initializer_list as parameter then look for constructors with set of arguments

8 / 12

C++1* Tech Talks

Uniform initialization 1 3 5

struct AggregateType { float x_ ; int y_ ; }; AggregateType scalar {0.43 f , 10};

7 9

17

struct N onAggregateType { int x_ ; double y_ ; N o n A ggregateType () {} N o n A ggregateType ( int x , double y ) : x_ { x } , y_ { y } {} }; N o n A g g r e gateType var2 {2 , 4.3};

19

// .

11 13 15

9 / 12

C++1* Tech Talks

Some training 1 3 5

struct AggregateType { float x_ = 0.1 f ; int y_ ; }; AggregateType a1 ;

// a1 . x_ ==

? , a1 . y_ == ?

7 9

17

struct N onAggregateType { int x_ = 7; double y_ ; N o n A ggregateType () {} N o n A ggregateType ( int x , double y ) : x_ { x } , y_ { y } {} }; N o n A g g r e gateType n1 ; // n1 . x_ == ? , n1 . y_ == ?

19

// .

11 13 15

10 / 12

C++1* Tech Talks

Some training 1 3 5 7

struct AggregateType { float x_ = 0.1 f ; int y_ ; }; AggregateType a1 ; AggregateType a2 {}; AggregateType a3 {0.43 f , 10};

// a1 . x_ == // a1 . x_ == // a1 . x_ ==

? , a1 . y_ == ? ? , a1 . y_ == ? ? , a1 . y_ == ?

9 11 13 15 17 19

struct N onAggregateType { int x_ = 7; double y_ ; N o n A ggregateType () {} N o n A ggregateType ( int x , double y ) : x_ { x } , y_ { y } {} }; N o n A g g r e gateType n1 ; // n1 . x_ == ? , n1 . y_ == ? N o n A g g r e gateType n2 {}; // n1 . x_ == ? , n1 . y_ == ? N o n A g g r e gateType n3 {2 , 4.3}; // n1 . x_ == ? , n1 . y_ == ?

10 / 12

C++1* Tech Talks

Some training 1 3 5 7

struct AggregateType { float x_ = 0.1 f ; int y_ ; }; AggregateType a1 ; AggregateType a2 {}; AggregateType a3 {0.43 f , 10};

// a1 . x_ == 0.1 , a1 . y_ == unitialized // a1 . x_ == 0.1 , a1 . y_ == 0 // a1 . x_ == 0.43 , a1 . y_ == 10

9 11 13 15 17 19

struct N onAggregateType { int x_ = 7; double y_ ; N o n A ggregateType () {} N o n A ggregateType ( int x , double y ) : x_ { x } , y_ { y } {} }; N o n A g g r e gateType n1 ; // n1 . x_ == 7 , n1 . y_ == unitialized N o n A g g r e gateType n2 {}; // n1 . x_ == 7 , n1 . y_ == unitialized N o n A g g r e gateType n3 {2 , 4.3}; // n1 . x_ == 2 , n1 . y_ == 4.3

10 / 12

C++1* Tech Talks

Summary I

design classes without constructors and use aggregate initialization (whenever possible)

I

for default values, use member initializers

I

always prefer brace initializers and don’t use parantheses anymore

I

be careful with the empty brace initializer (PODs/aggregates vs non-aggregates)

Bonus: Are () and {} initialization always the same for one type? 1

std :: vector < std :: string > v {100}; std :: vector < std :: string > v (100) ;

3 5

std :: vector < int > v {100}; std :: vector < int > v (100) ;

11 / 12

C++1* Tech Talks

Summary I

design classes without constructors and use aggregate initialization (whenever possible)

I

for default values, use member initializers

I

always prefer brace initializers and don’t use parantheses anymore

I

be careful with the empty brace initializer (PODs/aggregates vs non-aggregates)

Bonus: Are () and {} initialization always the same for one type? 1 3 5

std :: vector < std :: string > v {100}; std :: vector < std :: string > v (100) ; // but ... std :: vector < int > v {100}; std :: vector < int > v (100) ;

// v . size () == 100 , v [0] == "" // v . size () == 100 , v [0] == "" // v . size () == 1 , v [0] == 100 // v . size () == 100 , v [0] == 0

12 / 12

C++1* Tech Talks - GitHub

6 std::string p_sa = new std::string[5];. // -> elements are default initialized ... S s6. {1, {2, 3, {4, 5, 6, 7 } } }; // compile-time error, ill-formed. 18 int ai[] = {1, 2.0 }; .... be careful with the empty brace initializer (PODs/aggregates vs non-aggregates).

502KB Sizes 2 Downloads 316 Views

Recommend Documents

android sai tech - GitHub
Chapter 1: What is ANDROID 2. Chapter 2: Android Installation. Chapter 3: Android Application & Components. Chapter 4: AndroidManifest.xml. Chapter 5 : ...

android sai tech (2) - GitHub
Android is a mobile operating system that is based on a modified version of Linux. It was originally developed by a startup of the same name, .... Page 10 ...

PN532/C1 Near Field Communication (NFC) controller - GitHub
Sep 20, 2012 - to result in personal injury, death or severe property or environmental damage. NXP Semiconductors and its suppliers accept no liability for inclusion and/or use of NXP Semiconductors products in such equipment or applications and ther

Objective Education Experience Tech Skills - GitHub
Dec 21, 2016 - I am passionate about frontier tech, which is the most exciting part of technology. Love Reading Books and hungry for knowledge. Also i am ...

Tech Stack Boot Camp Day 1 - GitHub
Page 6 ... Naming URIs is key to usability [BEST PRACTICE]. • Nouns vs. verbs (things vs. actions). For example: ... POST (create, plus others). • OPTIONS (for ...

C1 - Bialek.pdf
received 1 dose) and vaccination of all eligible. persons without evidence of immunity. MMWR 2007;56(RR-4):1-39 available at www.cdc.gov. Page 3 of 32 ...

C1 - BOZZI.pdf
The paper analyzes the Law no. 76 of May 20th 2016 concerning “Regulations on civil unions. between persons of the same sex and discipline of cohabitation” that introduced in the Italian. legal system the protection of same-sex unions. After exam

C1 - Advanced.pdf
See “Alternative Writing Topics” in NorthStar Advanced, Reading and Writing, p. 44. Whoops! There was a problem loading this page. C1 - Advanced.pdf.

PN532/C1 - NXP Semiconductors
Sep 20, 2012 - Near Field Communication (NFC) controller. 6. Block diagram. Fig 1. .... byte indicates the length of the sent data bytes plus the LEN byte itself.

C1 - Payne.pdf
RotaTeqTM (Merck):. Bovine-human pentavalent. (G1, G2, G3, G4, P[8]). 3 doses @ 2, 4, 6 months. Rotarix® (GlaxoSmithKline):. Human monvalent. (G1, P[8]). 2 doses @ 2, 4 months. Live, attenuated oral. vaccines. Heterotypic immunity. against other str

Free Talks
Powerplaces are part of Ancient Heritage following on from the Mesolithic, celtic, roman, anglo-saxon and normanic past. Michael Reid, our guest speaker, is Author of “Powerplaces in Styria” and. “Powerplaces of South Wales” (soon to come) â€

c1.pdf
Whoops! There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. c1.pdf.

Money Talks
Dec 5, 2011 - §Rice University, P.O. Box 1892, Houston, TX 77251-1892, email: ... where K = R kidi is the aggregate production of the investment good and.

Talks in Jerusalem.docx - Groups
migration of arias. “Giuseppe Sarti – two different versions of Giulio Sabino” (Dr. Christin Heitmann). • concrete examples for possible changes and differences in ...

C1-consolicated notes.pdf
Connect more apps... Try one of the apps below to open or edit this item. C1-consolicated notes.pdf. C1-consolicated notes.pdf. Open. Extract. Open with. Sign In.

PAZ C1.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. PAZ C1.pdf.

TALKS ABOUT MAURITANIA.pdf
Sign in. Loading… Whoops! There was a problem loading more pages. Retrying... Whoops! There was a problem previewing this document. Retrying.

C1-Haumba-Sekikome.pdf
(USDL) built on D Space software, which was established with collaborative support of the. University of Bergen, University of Tennessee and Tufts University ...

C1 - Parker Fiebelkorn.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. C1 - Parker ...

TED Talks crowdsourcing.pdf
Download. Connect more apps... Try one of the apps below to open or edit this item. TED Talks crowdsourcing.pdf. TED Talks crowdsourcing.pdf. Open. Extract.

Brockway C1-C4.indd
Dale G. Brockway, Research Ecologist, USDA Forest Service, Southern. Research ...... To file a complaint of discrimination, write USDA, Director,. Office of Civil ...

Panorama-Pauta-C1-2016.pdf
Escalas. Hace algunas semanas, Stephen Hawking propuso junto con otros destacados cient ́ıficos y el millonario. ruso Yuri Milner el osado proyecto Starshot.

a-levels-c1-january-2009ms.pdf
Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. a-levels-c1-january-2009ms.pdf. a-levels-c1-january-2009ms.pdf.