First Post, Motivations, Some Notes
Published: 2019-01-03 18:14:38

---
title: First Post, Motivations, Some Notes
published: true
description: 
tags: 
---

# First Post
Hi there.

I've spent the morning looking for something like this and I'm glad to have found it. Never done any writing before, so this is my First Post.

# Motivations
I want to document the things I'm doing on a daily basis and the things that I'm learning. So these are my Motivations.

# Notes
And for the Notes, I'm just going to dive right into content that I'm learning
about right now, and review some items from Scott Meyers's Effective Modern C++. Keep in mind that as his descriptions are more informal, mine will tend to be as well, and I will most likely be very close in wording to what he has in the text. Also note that I will be reading the items in chronological order so I may come back to some posts to make edits and add notes about new things I learned later on.

So that this review is effective for me, I'm going to write as much as possible from memory and understanding, and then make it explicitly clear in the blog when I made reference to the text using the following format:

__RSB__ _Content that I did not recall and sourced entirely from the text_ __RSE__

and

__RCB__ _Content that I was unsure of and needed to use the text to confirm_
__RCE__

All content outside of these 'reference parentheses' is entirely from memory.

Let's go.

### First Review Item
How template type deduction works. There are three cases
that will allow you to deduce what `T` is in the templated function:
```c++
template <typename T> void f(ParamType t) {}
...
f(expr);
```
Each case relies on the form of `ParamType`:
1. `ParamType` is a reference or a pointer __RSB__ and not
   a universal reference __RSE__.
2. `ParamType` is  __RCB__ a universal reference __RCE__.
3. `ParamType` is neither a reference nor a pointer.

#### Case 1
In this case you follow these steps:
1. If `expr` is a reference, ignore the reference part.
2. Use the classic inspection technique (Meyers calls it pattern matching) to
   figure out what T should be.

##### Example
```c++
template <typename T> void f(T& t) {}
...
int x = 0;
int& r_x = x;
const int& rc_x = x;

f(x); // Since x is not a reference, nothing to ignore,
      // By inspection, we can determine that T must be int

f(r_x); // Since r_x is a reference, we ignore the reference part,
        // Then by inspection, we again determine that T must be int

f(rc_x); // Since rc_x is a reference, we ignore the reference part,
         // Then by inspection, we determine that T must be const int
```

#### Case 2
In this case, you follow these guidelines:
1. If `expr` is an lvalue, then both `ParamType` and `T` are of type lvalue
   reference. Note that this is strange for them __both__ to be lvalue
references.
2. If `expr` is an rvalue, then follow case 1 rules

##### Example
```c++
template <typename T> void f(T&& t) {}
...
int x = 0;
int& r_x = x;
const int& rc_x = x;

f(x); // x is an lvalue here, so T is int& and ParamType is also int&

f(r_x); // r_x is an lvalue, so T and ParamType are int&

f(rc_x); // rc_x is an lvalue, so T and ParamType are const int&

f(27); // 27 is an rvalue, so we follow case 1 rules to determine that T should
       // be int and so ParamType is RSB int&& RSE.
```

#### Case 3
In this case, we follow these steps:
1. Ignore reference.
2. __RCB__ Ignore any cv qualifiers __RCE__.

##### Example

```c++
template <typename T> void f(T t) {}
...
int x = 0;
const int c_x = x;
const int& rc_x = x;

f(x); // T and ParamType are int

f(c_x); // Ignore the const, so T and ParamType are int

f(rc_x); // Ignore the reference, then the const, so T and ParamType are int.
```

Now, we're done with the cases, but Meyers continues on with some other notes
relating to arrays decaying to pointers and functions decaying to function
pointers.

##### Arrays decaying to pointers
__RSB__
Meyers notes that we often believe that pointers and arrays are
interchangeable, according to the following examples:

```c++
// Example 1
void func(int param[]);

// The above declaration is actually equivalent to this declaration using a pointer
void func(int *param);

// Example 2
const int important_numbers[] = {1, 25, 125};

const int * ptr_to_important_numbers = important_numbers; // array decays to pointer

// Example 3 (uses important_numbers declared in previous example)
template <typename T> void func(T t) {}

func(important_numbers); // T is deduced to be const int * here
```

However, if the form of the parameter type in a template is a reference, `T` will
actually be deduced to be an array of the correct type, where the type also
includes the size of the array. So, if we modified the above Example 3 to be

```c++
template <typename T> void func(T& t) {}

func(important_numbers);
```

`T` would successfully be deduced to be `const int (&)[3]`. This allows you to
deduce the size of arrays at compile time interestingly enough:

```c++
template <typename T, int N> constexpr std::size_t sizeOfArray(T (&)[N])
noexcept {
  return N;
}
```

##### Functions decaying to function pointers
He also notes similar things that happen to functions:

```c++
template <typename T> void f1(T t) {}

template <typename T> void f2(T& t) {}

void myFunc(const char*, int); // myFunc has type void(const char*, int)

f1(myFunc); // T is deduced to type void (*)(const char*, int)
f2(myFunc); // T is deduced to type void (&)(const char*, int)
```