0%

This is my learning notes from the course - Mastering C++ Standard Library Features.

Perfect Forwarding

  • Forwarding references
  • std::forward

When no template argument deduction is happening, & means “lvalue reference“ and && means “rvalue reference“.

1
2
void take_lvalue(int&);
void take_rvalue(int&&);

In the context of template argument deduction, && has a different meaning: “forwarding reference“.

1
2
template <typename T>
void take_anything(T&&);

In the function signature above, T&& does NOT mean rvalue reference. Instead, it means “forwarding reference“.

A “forwarding reference“ binds to everything and remembers the original value category of the passed object. Here’s an example:

Read more »

This is my learning notes from the course - Mastering C++ Standard Library Features.

Xvalues? Glvalues? Prvalue?

Last time, we talked about Value Categories and Move Semantics in C++, and got some good ideas on lvalues and rvalues with examples. Today, let’s dig into more details on value categories in C++.

While knowledge of lvalues and rvalues is almost always enough for day-to-day coding, it provides a simplified model of C++ value categories.

Knowledge of the full picture is helpful to get a deeper understanding of move semantics and to write advanced code.

Value Categories (Simplified)

1
2
3
4
5
6
7
8
     Expression
a + 5 * b

/ \
/ \
/ \

Lvalue Rvalue
Read more »

This is my learning notes from the course - Mastering C++ Standard Library Features.

Move Semantics in the Standard Library

  • Many classes in the standard library are move aware
  • Some classes represent unique ownership and are move-only
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct foo {};
void pair_and_tuple()
{
// General-purpose utility classes such as `std::pair` and `std::tuple`
// are move-aware.

std::pair<foo, int> p;
auto p_copy = p;
auto p_move = std::move(p);
// http://en.cppreference.com/w/cpp/utility/pair/pair

std::tuple<foo, int, char> t;
auto t_copy = t;
auto t_move = std::move(t);
// http://en.cppreference.com/w/cpp/utility/tuple/tuple

// If the items contained in them have valid move operations, they will be
// properly used when moving the pair/tuple.
}
Read more »

Machine Learning

Machine Learning isn’t new; it has been around at least since the 1970’s, when the first related algorithm appeared. What has changed is that the explosion in computing power has allowed us to use machine learning to tackle ever-more-complex problems, while explosion of data being captured and stored has allowed us to apply machine learning to an ever-expanding range of domains.

The general idea behind most machine learning is that a computer learns to perform a task by studying a training set of examples. The computer (or system of distributer or embedded computers and controllers) then performs the same task with data it hasn’t encountered before.

Read more »

This is my learning notes from the course - Mastering C++ Standard Library Features.

Lvalues and Rvalues

Why to talk about Lvalues and Rvalues in C++?

Knowledge of value categories and references is necessary to understand move semantics and ownership transfer.

Move Semantics - Sneak Peek

  • Move semantics revolve around the idea of transferring ownership of resources instead of copying them.
  • They increase performance, safety, and readability of libraries and applications.
Read more »