Summary of Effective Modern C++: Items 1 & 2
Summary of Effective Modern C++: Items 1 & 2
Modern C++
Items 1 & 2
int x = 27;
const int cx = x;
const int& rx = x;
x = 20;
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);
Template<typename T>
Void func(T* param);
Int x = 27;
const int* px = &x;
X x;
X foobar();
Template<typename T>
Void func(T&& param);
template<typename T>
void func(T param);
template<typename T>
void func(T& param);
template<typename T>
void f1(T param);
template<typename T>
void f2(T& param);
void func(int);
template<typename T>
void f(T param);
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}