SlideShare a Scribd company logo
1 of 26
Summary of Effective
Modern C++
Items 1 & 2
(Homework Assignment – YoungHa Kim)
Item 1
Template Type Deduction
Lvalue, Rvalue
• Definition
• Lvalue, is a value with a specific location in memory (think of it as a
location value)
• Rvalue is a value that is not an Lvalue.
• Generally, a temporary variable or value.
Int, Const int, Const int&
• Let’s execute the following code: (can only modify x,
• otherwise you get a compiler error, cannot modify const)
int x = 27;
const int cx = x;
const int& rx = x;
printf("x=%d, cx=%d, rx=%d n", x, cx, rx);
x = 20;
printf("x=%d, cx=%d, rx=%d n", x, cx, rx);
Int, Const int, Const int&
Template Type Deduction
• Let’s refer to the following code example:
• Deduced types will depend on the form of Paramtype and
expr, which can be divided into 3 cases.
Template<typename T>
Void func(Paramtype param);
Func(expr)
Case 1: Paramtype is a Reference or
Pointer, but not a Universal Reference
• In this case:
• If expr is a reference, ignore the reference
• Pattern-match expr’s type against Paramtype to determine T
Template<typename T>
Void func(T& param);
Int x = 27; const int CX = x; const int& RX = x;
Func(x); // T is int, paramtype is int&
Func(CX); // T is const int, paramtype is const int&
Func(RX); // T is const int, paramtype is const int&
Case 1: Works the same for pointers
• In this case:
Template<typename T>
Void func(T* param);
Int x = 27;
const int* px = &x;
Func(&x); // T is int, paramtype is int*
Func(px); // T is const int, paramtype is const int*
Rvalue Reference
• If X is any type, then X&& is called an rvalue reference to X. For
better distinction, the ordinary reference X& is now also called an
lvalue reference.
void foo(X& x); // lvalue reference overload
void foo(X&& x); // rvalue reference overload
X x;
X foobar();
foo(x); // argument is lvalue: calls foo(X&)
foo(foobar()); // argument is rvalue: calls foo(X&&)
Universal Reference
• If a variable or parameter is declared to have type T&& for
some deduced type T, that variable or parameter is a
universal reference.
• Things that are declared as rvalue reference can be lvalues or
rvalues. The distinguishing criterion is: if it has a name, then
it is an lvalue. Otherwise, it is an rvalue.
Case 2: Paramtype is a Universal
Reference
• Type deduction for universal reference parameters are
different for Lvalues and Rvalues.
• This never happens for non-universal references.
Case 2: Paramtype is a Universal
Reference
• If expr is an Lvalue, both T and Paramtype are deduced to be
Lvalue references. (If Rvalue, then case 1 applies)
Template<typename T>
Void func(T&& param);
Int x = 27; const int CX = x; const int& RX = x;
Func(x); // x is Lvalue, so T is int&, paramtype is int&
Func(CX); // CX is Lvalue, so T is const int&, paramtype is const int&
Func(RX); // RX is Lvalue, so T is const int&, paramtype is const int&
Func(27); // 27 is Rvalue, so T is int, paramtype is int&&
Case 3: Paramtype is neither a pointer
or reference
• We are dealing with pass by value
• This means param will be a copy of whatever is passed in
• A completely new object
• If expr’s type is a reference, ignore the reference
• If after ignoring reference, expr is const or volatile, ignore
const or volatile.
Case 3: Paramtype is neither a pointer
or reference
template<typename T>
void func(T param);
int x = 27; const int cx = x; const int rx = x;
func(x); // T and param are both int
func(cx); // T and param are both int
func(rx); // T and param are both int
• param is not const, because it is a copy of cx or rx.
Case 3: Paramtype is neither a pointer
or reference
template<typename T>
void func(T param);
const char* const ptr = “asdf”; // const pointer to a const object
// const pointer can’t point to a different location and cannot be null
func(ptr); // param will be const char*
• constness of ptr is ignored when copied to the new pointer param,
but constness of what ptr points to is preserved.
Passing Array Arguments
const char name[] = “YH Kim”; // char[7]
const char *ptrName = name; // array decays to ptr
template<typename T>
void func(T param);
func(name); // what happens??
Passing Array Arguments
C++ handles
void myFunc(int param[]);
void myFunc(int* param);
as the same function (almost).
func(name); // array parameters are treated as pointer
parameters, so the value is deduced as a pointer type. In this
case const char*
Passing Array Arguments
• functions cannot declare parameters that are arrays, but can
declare parameters that are pointers to arrays.
template<typename T>
void func(T& param);
func(name); // pass array to func
• the type deduced is the actual type of the array.
• so type of func’s parameter is const char(&)[7]
Function Arguments
• Functions can also decay into pointers.
template<typename T>
void f1(T param);
template<typename T>
void f2(T& param);
void func(int);
f1(func); // param is deduced as pointer to func, void*(int)
f2(func); // param is deduced as ref to func, void&(int)
Item 2
Understand auto type deduction
Auto vs Template Type Deduction
• Basically the same thing
• If we have:
auto x = 2;
• this is handled as:
template<typename T> // conceptual template for deducing auto
void func(T param);
func(2); // param’s type is auto’s type
Auto vs Template Type Deduction
• If we have:
const auto& rx = x;
• this is handled as:
template<typename T> // conceptual template for deducing auto
void func(const T& param);
func(x); // param’s type is auto’s type
Exceptions
• Auto deduction works the same as template deduction with
the following exceptions
int x1 = 33; -> auto x1 = 33;
int x2(33); -> auto x2(33);
int x3 = {33}; -> auto x3 = {33};
int x4 {33}; -> auto x4{33};
• x1 and x2 declare a variable of type int with value 33
• x3 and x4 declare a var of type std::initializer_list<int> with a
single element having value 33.
Using { } initializers for auto
• Using braced initializers for auto always instantiates
std::initializer_list.
auto x = { 1, 2, 3}; // x’s type is std::initializer_list<int>
template<typename T>
void f(T param);
f( {1,2,3} ); // error! can’t deduce type for T
Using { } initializers for auto
• Working code
template<typename T>
void f(std::initializer_list<T> param);
f( {1,2,3} );
// T is deduced as int, and param is std::initializer_list<int>
C++ 14
• C++ 14 allows auto on function return type
auto createInitList()
{
return { 1,2,3 }; // error! can’t deduce type for {1,2,3}
}
• this use of auto employs template type deduction, so above code
fails.
• the same is true when auto is used in a parameter type specification
in a C++ 14 lambda
std::vector<int> v;
auto resetV = [&v](const auto& newValue) { v = newValue; };
resetV( {1,2,3} ); // error! can’t deduce type for {1,2,3}

More Related Content

What's hot

Chp3(pointers ref)
Chp3(pointers ref)Chp3(pointers ref)
Chp3(pointers ref)Mohd Effandi
 
Advance topics of C language
Advance  topics of C languageAdvance  topics of C language
Advance topics of C languageMehwish Mehmood
 
[OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue Referen...
[OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue Referen...[OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue Referen...
[OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue Referen...Andrey Upadyshev
 
Regular Expressions in Java
Regular Expressions in JavaRegular Expressions in Java
Regular Expressions in JavaOblivionWalker
 
An Introduction : Python
An Introduction : PythonAn Introduction : Python
An Introduction : PythonRaghu Kumar
 
Strings-Computer programming
Strings-Computer programmingStrings-Computer programming
Strings-Computer programmingnmahi96
 
C traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmersC traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmersRichard Thomson
 
11. using regular expressions with oracle database
11. using regular expressions with oracle database11. using regular expressions with oracle database
11. using regular expressions with oracle databaseAmrit Kaur
 
Hot С++: Universal References And Perfect Forwarding
Hot С++: Universal References And Perfect ForwardingHot С++: Universal References And Perfect Forwarding
Hot С++: Universal References And Perfect ForwardingAndrey Upadyshev
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointersSamiksha Pun
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular ExpressionMasudul Haque
 

What's hot (19)

Void pointer in c
Void pointer in cVoid pointer in c
Void pointer in c
 
Chp3(pointers ref)
Chp3(pointers ref)Chp3(pointers ref)
Chp3(pointers ref)
 
Advance topics of C language
Advance  topics of C languageAdvance  topics of C language
Advance topics of C language
 
[OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue Referen...
[OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue Referen...[OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue Referen...
[OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue Referen...
 
M C6java7
M C6java7M C6java7
M C6java7
 
Regular Expressions in Java
Regular Expressions in JavaRegular Expressions in Java
Regular Expressions in Java
 
An Introduction : Python
An Introduction : PythonAn Introduction : Python
An Introduction : Python
 
Lecture 17 - Strings
Lecture 17 - StringsLecture 17 - Strings
Lecture 17 - Strings
 
Strings-Computer programming
Strings-Computer programmingStrings-Computer programming
Strings-Computer programming
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
C traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmersC traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmers
 
11. using regular expressions with oracle database
11. using regular expressions with oracle database11. using regular expressions with oracle database
11. using regular expressions with oracle database
 
Hot С++: Universal References And Perfect Forwarding
Hot С++: Universal References And Perfect ForwardingHot С++: Universal References And Perfect Forwarding
Hot С++: Universal References And Perfect Forwarding
 
LISP:Predicates in lisp
LISP:Predicates in lispLISP:Predicates in lisp
LISP:Predicates in lisp
 
M C6java2
M C6java2M C6java2
M C6java2
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular Expression
 
LISP: Type specifiers in lisp
LISP: Type specifiers in lispLISP: Type specifiers in lisp
LISP: Type specifiers in lisp
 
C pointer
C pointerC pointer
C pointer
 

Similar to Summary of effective modern c++ item1 2

[OLD VERSION, SEE DESCRIPTION FOR THE NEWER VERSION LINK] Hot С++: Universal ...
[OLD VERSION, SEE DESCRIPTION FOR THE NEWER VERSION LINK] Hot С++: Universal ...[OLD VERSION, SEE DESCRIPTION FOR THE NEWER VERSION LINK] Hot С++: Universal ...
[OLD VERSION, SEE DESCRIPTION FOR THE NEWER VERSION LINK] Hot С++: Universal ...Andrey Upadyshev
 
Chapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programmingChapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programmingz9819898203
 
Introduction to C Programming - R.D.Sivakumar
Introduction to C Programming -  R.D.SivakumarIntroduction to C Programming -  R.D.Sivakumar
Introduction to C Programming - R.D.SivakumarSivakumar R D .
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniquesvalarpink
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants Hemantha Kulathilake
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - PointersWingston
 
12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cppsharvivek
 
java Basic Programming Needs
java Basic Programming Needsjava Basic Programming Needs
java Basic Programming NeedsRaja Sekhar
 
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdfBasic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdfComputer Programmer
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingEric Chou
 
C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3Rumman Ansari
 
C language presentation
C language presentationC language presentation
C language presentationbainspreet
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Sheik Uduman Ali
 

Similar to Summary of effective modern c++ item1 2 (20)

Effective Modern C++
Effective Modern C++Effective Modern C++
Effective Modern C++
 
Token and operators
Token and operatorsToken and operators
Token and operators
 
[OLD VERSION, SEE DESCRIPTION FOR THE NEWER VERSION LINK] Hot С++: Universal ...
[OLD VERSION, SEE DESCRIPTION FOR THE NEWER VERSION LINK] Hot С++: Universal ...[OLD VERSION, SEE DESCRIPTION FOR THE NEWER VERSION LINK] Hot С++: Universal ...
[OLD VERSION, SEE DESCRIPTION FOR THE NEWER VERSION LINK] Hot С++: Universal ...
 
C language
C languageC language
C language
 
lecture2.ppt
lecture2.pptlecture2.ppt
lecture2.ppt
 
Chapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programmingChapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programming
 
Introduction to C Programming - R.D.Sivakumar
Introduction to C Programming -  R.D.SivakumarIntroduction to C Programming -  R.D.Sivakumar
Introduction to C Programming - R.D.Sivakumar
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants
 
C language basics
C language basicsC language basics
C language basics
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 
12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp
 
java Basic Programming Needs
java Basic Programming Needsjava Basic Programming Needs
java Basic Programming Needs
 
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdfBasic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary Programming
 
C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
C language presentation
C language presentationC language presentation
C language presentation
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 

Recently uploaded

Broad bean, Lima Bean, Jack bean, Ullucus.pptx
Broad bean, Lima Bean, Jack bean, Ullucus.pptxBroad bean, Lima Bean, Jack bean, Ullucus.pptx
Broad bean, Lima Bean, Jack bean, Ullucus.pptxjana861314
 
Chemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfChemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfSumit Kumar yadav
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsAArockiyaNisha
 
Presentation Vikram Lander by Vedansh Gupta.pptx
Presentation Vikram Lander by Vedansh Gupta.pptxPresentation Vikram Lander by Vedansh Gupta.pptx
Presentation Vikram Lander by Vedansh Gupta.pptxgindu3009
 
GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)Areesha Ahmad
 
Animal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxAnimal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxUmerFayaz5
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...jana861314
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxkessiyaTpeter
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTSérgio Sacani
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Lokesh Kothari
 
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...Sérgio Sacani
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Sérgio Sacani
 
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisRaman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisDiwakar Mishra
 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoSérgio Sacani
 
GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)Areesha Ahmad
 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bSérgio Sacani
 
GFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptxGFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptxAleenaTreesaSaji
 
Chromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATINChromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATINsankalpkumarsahoo174
 
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 60009654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000Sapana Sha
 

Recently uploaded (20)

Broad bean, Lima Bean, Jack bean, Ullucus.pptx
Broad bean, Lima Bean, Jack bean, Ullucus.pptxBroad bean, Lima Bean, Jack bean, Ullucus.pptx
Broad bean, Lima Bean, Jack bean, Ullucus.pptx
 
Chemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfChemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdf
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based Nanomaterials
 
Engler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomyEngler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomy
 
Presentation Vikram Lander by Vedansh Gupta.pptx
Presentation Vikram Lander by Vedansh Gupta.pptxPresentation Vikram Lander by Vedansh Gupta.pptx
Presentation Vikram Lander by Vedansh Gupta.pptx
 
GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)
 
Animal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxAnimal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptx
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOST
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
 
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
 
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisRaman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on Io
 
GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)
 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
 
GFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptxGFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptx
 
Chromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATINChromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATIN
 
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 60009654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
 

Summary of effective modern c++ item1 2

  • 1. Summary of Effective Modern C++ Items 1 & 2 (Homework Assignment – YoungHa Kim)
  • 3. Lvalue, Rvalue • Definition • Lvalue, is a value with a specific location in memory (think of it as a location value) • Rvalue is a value that is not an Lvalue. • Generally, a temporary variable or value.
  • 4. Int, Const int, Const int& • Let’s execute the following code: (can only modify x, • otherwise you get a compiler error, cannot modify const) int x = 27; const int cx = x; const int& rx = x; printf("x=%d, cx=%d, rx=%d n", x, cx, rx); x = 20; printf("x=%d, cx=%d, rx=%d n", x, cx, rx);
  • 5. Int, Const int, Const int&
  • 6. Template Type Deduction • Let’s refer to the following code example: • Deduced types will depend on the form of Paramtype and expr, which can be divided into 3 cases. Template<typename T> Void func(Paramtype param); Func(expr)
  • 7. Case 1: Paramtype is a Reference or Pointer, but not a Universal Reference • In this case: • If expr is a reference, ignore the reference • Pattern-match expr’s type against Paramtype to determine T Template<typename T> Void func(T& param); Int x = 27; const int CX = x; const int& RX = x; Func(x); // T is int, paramtype is int& Func(CX); // T is const int, paramtype is const int& Func(RX); // T is const int, paramtype is const int&
  • 8. Case 1: Works the same for pointers • In this case: Template<typename T> Void func(T* param); Int x = 27; const int* px = &x; Func(&x); // T is int, paramtype is int* Func(px); // T is const int, paramtype is const int*
  • 9. Rvalue Reference • If X is any type, then X&& is called an rvalue reference to X. For better distinction, the ordinary reference X& is now also called an lvalue reference. void foo(X& x); // lvalue reference overload void foo(X&& x); // rvalue reference overload X x; X foobar(); foo(x); // argument is lvalue: calls foo(X&) foo(foobar()); // argument is rvalue: calls foo(X&&)
  • 10. Universal Reference • If a variable or parameter is declared to have type T&& for some deduced type T, that variable or parameter is a universal reference. • Things that are declared as rvalue reference can be lvalues or rvalues. The distinguishing criterion is: if it has a name, then it is an lvalue. Otherwise, it is an rvalue.
  • 11. Case 2: Paramtype is a Universal Reference • Type deduction for universal reference parameters are different for Lvalues and Rvalues. • This never happens for non-universal references.
  • 12. Case 2: Paramtype is a Universal Reference • If expr is an Lvalue, both T and Paramtype are deduced to be Lvalue references. (If Rvalue, then case 1 applies) Template<typename T> Void func(T&& param); Int x = 27; const int CX = x; const int& RX = x; Func(x); // x is Lvalue, so T is int&, paramtype is int& Func(CX); // CX is Lvalue, so T is const int&, paramtype is const int& Func(RX); // RX is Lvalue, so T is const int&, paramtype is const int& Func(27); // 27 is Rvalue, so T is int, paramtype is int&&
  • 13. Case 3: Paramtype is neither a pointer or reference • We are dealing with pass by value • This means param will be a copy of whatever is passed in • A completely new object • If expr’s type is a reference, ignore the reference • If after ignoring reference, expr is const or volatile, ignore const or volatile.
  • 14. Case 3: Paramtype is neither a pointer or reference template<typename T> void func(T param); int x = 27; const int cx = x; const int rx = x; func(x); // T and param are both int func(cx); // T and param are both int func(rx); // T and param are both int • param is not const, because it is a copy of cx or rx.
  • 15. Case 3: Paramtype is neither a pointer or reference template<typename T> void func(T param); const char* const ptr = “asdf”; // const pointer to a const object // const pointer can’t point to a different location and cannot be null func(ptr); // param will be const char* • constness of ptr is ignored when copied to the new pointer param, but constness of what ptr points to is preserved.
  • 16. Passing Array Arguments const char name[] = “YH Kim”; // char[7] const char *ptrName = name; // array decays to ptr template<typename T> void func(T param); func(name); // what happens??
  • 17. Passing Array Arguments C++ handles void myFunc(int param[]); void myFunc(int* param); as the same function (almost). func(name); // array parameters are treated as pointer parameters, so the value is deduced as a pointer type. In this case const char*
  • 18. Passing Array Arguments • functions cannot declare parameters that are arrays, but can declare parameters that are pointers to arrays. template<typename T> void func(T& param); func(name); // pass array to func • the type deduced is the actual type of the array. • so type of func’s parameter is const char(&)[7]
  • 19. Function Arguments • Functions can also decay into pointers. template<typename T> void f1(T param); template<typename T> void f2(T& param); void func(int); f1(func); // param is deduced as pointer to func, void*(int) f2(func); // param is deduced as ref to func, void&(int)
  • 20. Item 2 Understand auto type deduction
  • 21. Auto vs Template Type Deduction • Basically the same thing • If we have: auto x = 2; • this is handled as: template<typename T> // conceptual template for deducing auto void func(T param); func(2); // param’s type is auto’s type
  • 22. Auto vs Template Type Deduction • If we have: const auto& rx = x; • this is handled as: template<typename T> // conceptual template for deducing auto void func(const T& param); func(x); // param’s type is auto’s type
  • 23. Exceptions • Auto deduction works the same as template deduction with the following exceptions int x1 = 33; -> auto x1 = 33; int x2(33); -> auto x2(33); int x3 = {33}; -> auto x3 = {33}; int x4 {33}; -> auto x4{33}; • x1 and x2 declare a variable of type int with value 33 • x3 and x4 declare a var of type std::initializer_list<int> with a single element having value 33.
  • 24. Using { } initializers for auto • Using braced initializers for auto always instantiates std::initializer_list. auto x = { 1, 2, 3}; // x’s type is std::initializer_list<int> template<typename T> void f(T param); f( {1,2,3} ); // error! can’t deduce type for T
  • 25. Using { } initializers for auto • Working code template<typename T> void f(std::initializer_list<T> param); f( {1,2,3} ); // T is deduced as int, and param is std::initializer_list<int>
  • 26. C++ 14 • C++ 14 allows auto on function return type auto createInitList() { return { 1,2,3 }; // error! can’t deduce type for {1,2,3} } • this use of auto employs template type deduction, so above code fails. • the same is true when auto is used in a parameter type specification in a C++ 14 lambda std::vector<int> v; auto resetV = [&v](const auto& newValue) { v = newValue; }; resetV( {1,2,3} ); // error! can’t deduce type for {1,2,3}