0% found this document useful (0 votes)
7 views

C++ (11) Docs

Uploaded by

Rounak Polley
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

C++ (11) Docs

Uploaded by

Rounak Polley
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

C++(11) features – part 1

1. What are the contexts “auto” can be used?

2.

3. Decltype syntax? How it works?

4.

5.

6. Auto stripes out all the cv-qualifiers from a declaration. (like const, &, * anything).

That’s why we have to explicitly add that in case of “auto”.

Const auto& x = I;

7. But decltype automatically deduces the complete type without evaluating the value.

s
8. Auto and decltype conversion happened in compile time. Now what compiler interpret the
type for auto or decltype, no ways to know.

But here is two hacks:

 form the error message, we can see the compiler interpreted type.

 We can also use typeid to know the compiler interpreted type. But it is in a compiler
dependent format which is different compiler to compiler, which is really hard to decode
the type from the format.

9. Suffix/ Trailing Return type

10. C++ (14) upgradation in place of Suffix/ Trailing Return type.


11.

 We can assign on a reference.


 We have to assign on a reference.

Summary:
1. auto
2. decltype
3. decltype(auto)
4. compiler deduced type (error msg / typeid)
5. suffix / trailing return type (old – new)
6. The example
C++(11) features – part 2

1. Initializer list

2. See the custom vector class.

3. std::initializer list stores initializer values in an array and offers these member functions:
◦ size // # of elements in the array
◦ begin // ptr to first array element
◦ end

4.
5. vector<double> v1{7};

 this means invoking the initializer list.


 If you don’t create a template that accepts an initializer list, it will not work.

6. Overload resolution(very imp)

7.

8.

9. Uniform initialization syntax.

 Using {} curly braces.


 In case of const or pointer type array declaration it is really helpful.
10. Aggregate:

Error: too many initializers

11. Non-aggregate (We have to give exactly same parameter in this case)

Error: too many initializers


, too few initializers

12.

13.

14. Narrowing is not possible in case of initializer list.


15. Range for operator.

Summary:

1. For function calling, () this must be used, f {1, 2} this is totally not supported. This brace
is only for initialization purpose, not invoking functions.

2. Initializer list

3. Overload Resolution (Narrowing is not possible in {} But possible for ()) *

4. Auto can deduce initializer list but template cannot.*

5. Uniform syntax using {}

6. Aggregate vs non-aggregate

7. Brace initializer can use = {} but not in every case.

8. No – 13 – explicit wala example💩*

9. Narrowing is not possible in initializer list.*

10. Range for operator


C++(11) features – part 3

1. Constexpr : when we need to calculate an expression at the compile time, then we should
use constexpr as a suffix.

 If operator | is not initialized at the compile time, the switch statement gives an error.

2.

 Because we will get the value of f3 at run time, so constexpr is not applicable here.

3. M – 48.10 - const

4. Static constexpr

5.

 As vector is resizable.so, vector’s size is not a compile time contant.

6.
 array is of fixed size, so it can be done.

7. Noexcept and its usages (ChatGPT)

 https://chatgpt.com/c/6711d8eb-8a0c-8009-a258-277b920273b5

8.

9. nullptr vs NULL vs 0 (Regular function)

0 -> int

NULL -> may be integer, may be a pointer to 0. (Here its pointer, so ambiguity)

Nullptr -> int*

10.

 0 -> int

NULL -> long int

Nullptr -> okay! (template)

11. C++(11 ) inline namespace(make things global)***

12. C++(03) inline namespace implementation using “using”.***

13. Static_assert

s
14. user-defined literals

 read the codes. (double is not allowed, use long double as signature)

15. Digit separators and binary literals.

16. Raw string literals

 Here u don’t need to use escape characters. Here we can easily print multi lines.

s
17. Some more: https://chatgpt.com/c/6711eafc-7c0c-8009-9286-8f8078797305

Summary:

1. Constexpr
 Const, static, vector, array
2. Noexcept
3. 0 vs NULL vs nullptr (Reg func. vs template forwarding)
4. Inline namespace
5. Static_assert
6. User defined literals(long double)**
7. Digit separators and binary literals
8. Raw string
9. more
10.
C++(11) features – part 4

1. Copying vs moving

 swapping by move
2. lvalue and rvalue.

 Lvalue

 Rvalue

 Examples.

3. Value movement is unsafe when the source is a lvalue object.

Value movement is safe when the source is a rvalue object.

Source lvalue: copy needed.

Source rvalue: move okay!

4. Lvalues may bind to lvalue references:

5. Rvalues may bind to lvalue references to const:

6. Rvalues may bind to rvalue references to non-const:

7. Lvalues may not bind to rvalue references:


8. Rvalue reference slide(practice)

9. Rvalue References and const

 Rvalue references (T&&) are primarily intended to enable move semantics. A const
rvalue reference (const T&&) means that the rvalue reference points to a constant
object, which cannot be modified.
 This means you cannot move from it because moving inherently involves altering the
state of the original object (leaving it in an unspecified state).
 So, const rvalue reference commonly not used!!

10. Slide – 21, 22


 What is RVO? (see in case of function return Move ctor is invoked, which is RVO)

11. Copy: (construct -> delegation(same) -> copy -> destruct)

Move: (construct -> move -> dtor: nullptr)

s
 In move semantics, dtor = nullptr as we are moving, that means we are using the existing
resource.
 So, that will be destroyed only after full execution of the code.

Summary:

1. Only see RVO slide and vector slide.


C++(11) features – part 5

1.

2. Move semantics in case of built-in data type.


3. In case of user defined types.

 mRrc = c.mRrc; still copying thing. As c has a address, it is a lvalue, which matches with
the copy constructor version not with the move one.

 same for this also, as c is a lvalue.

4.

 Solution: std::move takes lvalue and returns rvalue.


5.

 reference collapsing in cpp says that if any one of the references is a lvalue reference, the
resultant will be a lvalue reference.
 If both are rvalue reference, then only the resultant will be a rvalue reference.

6. Std::move return type. (Should always return a rvalue..now how to ensure that?)

7. Parameter type.

 So only solution is overloading.



8. Suppose a template declared with typename T&& (rvalue reference). In the body it has both
lvalue and rvalue constructor. Now, what it will invoke?

 Based on reference collapsing rule.


 Slide – 21

9. The return type of a lvalue ref should be lvalue and rvalue -> rvalue.

Summary:

Std::move takes both lvalue and rvalue(ovaerload)

But return only rvalue.(remove ref)


C++(11) features – part 6

1. Summary:

 If it is a regular function, a rvalue reference only takes non-const rvalue.

 If it is a function respect to the context of templates, rvalue reference tales takes lvalue
or rvalue, const or non-const. As it follows the rules of reference collapsing.

2. auto type deduction vs decltype type deduction.

 Auto follows the collapsing rule.

 Decltype doesn’t use template type deduction. Both are int.

s
3. Perfect forwarding problem

 Every forwarded function calls the copy constructor.

4. solution: std::forward

5. Type safety
 That means perfect forwarding is totally type-safe.
C++(11) features – part 7

1. Lambda expression(void return type)

2. Lambda expression(return type)

3. Instant calling.

4. call by Reference.
5. = operator in capture clause.

 It will copy all the variables defined in the scope only which are required in the lambda.
(before the lambda).

6. When it is captured and when not?

 No capture as t is not used in the lambda.so no copying occurred.

 here we are using &. So, no copy occurred.


 The copy is for auto m2 = m1.
7.

8.

 The second point.

9.

 Read this carefully.

10. Mutable and non-mutable lambda in case of capture-by value(ChatGPT)


11. Slide: 22, 23 : = may create infinite loop.

TIPS: when we are modifying something and that is associated with lambda then we must
use &.Otherwise we can use =.
12.

 In case of mutable, we can change value locally using capture by value. The change made
in the expression body doesn’t reflect on the original one.

13. Slide-26 example***

14. = & usecases

15. Restrictions on capture.


C++(11) features – part 8

1. Std::function

2. Slide – 9

3. Pipelining

4. Generalized lambda in c++.

 Using template or auto.

 Slide 13: for generic lambda, we need to explicitly convert const char* to string.

s
5. Capture-less Generic λ can be conv. to Function Pointers.

6. Recursive λ expression.

 Problem:

 Solution: (capture clause solution)

or, (parameter list solution)


7. Generalized lambda captures.

Summary:
 First example (why m_ will not work, this or = work)
 Std::function
 Pipeline
 Generic lambda
 Capture less generalized lambda -> function pointers
 Recursive lambda problem & sol.
 Generalized lambda captures
C++(11) features – part 9

1. =default and =delete Functions


 https://chatgpt.com/c/6720f0ba-1f08-8009-bb3a-c519d37961c5

2. Control of Default Move and Copy.☹

 Rule of five
 Rule of three
 Rule of zero

 https://chatgpt.com/c/6714fbcb-0cf4-8009-b182-275d4a5d3175

3. Delegating constructors

 https://chatgpt.com/c/6720f0ba-1f08-8009-bb3a-c519d37961c5

4. In class member initializers.(Act as default parameter)

 https://chatgpt.com/c/6720f0ba-1f08-8009-bb3a-c519d37961c5

5. Inheriting Constructors (using using)

 https://chatgpt.com/c/6720f0ba-1f08-8009-bb3a-c519d37961c5
 VVI- PPT example

6. Override Controls – virtual & final

7. explicit Conversion Operators(VVI)

 implicit conversions will not be permitted

https://chatgpt.com/c/671500b1-20a4-8009-a783-a3c5f599ee34
https://chatgpt.com/c/6721025e-7a5c-8009-a21e-b12390e039be (BOOL)
C++(11) features – part 10

1. enum in c++(11).

2.

3. It is scoped. Can’t be accessed globally.


4. conversion from int to Enum-type is not possible.
5. Enum-type to int is possible in only for non-class enum.
6. For class based enum no conversion is allowed.

7. Enum class underlying type.

8.

9. If you give a value greater in size than the underlying type, it will show a error.

10.
11. In c++(03), for forward declaration you must give a size.
12. In c++(11), the underlying type is int by default.

13. Fixed size integer types.


14. https://chatgpt.com/c/6715d219-07f8-8009-aa13-c41826673cb9

15.

16. Generalized unions-Which can store different type of values.

You might also like