Moule Lin
← All notes

Variadic Templates, auto and Range-Based for in C++

Variadic template parameters

template <class T1, class... args>
void test(T1 t, args&... arg){

}
test(1,2,'a','c');

auto

list<string> c;
auto ite = find(c.begin(), c.end(),traget);
//A variable declared with auto must be assigned at declaration
auto ite; //this line and the next one are wrong
itr = find(c.begin(), c.end(),traget);

or
list<string>::interate ite = find(c.begin(), c.end(),traget);

ranged-base for

for (decl:coll){
    statement
}
for(auto elem:vec){ //each element is taken out and assigned to the left
    // the right side is the container, the left side is each output element
    cout << elem << endl;
}
//a reference works too
for(auto& elem:vec){
    // the right side is the container, the left side is each output element
    cout << elem << endl;
}

for (int i:{1,2,3,4,5}){
    cout << i;
}

Pass by reference and pass by value have the same signature

The signature is everything except the return type
double image(const double& in) const {}
double image(const double in) const {}

The two above are the same and cannot coexist

notes: the const after image() is also part of the signature,
double image(const double& in) const {}
double image(const double in) {}

These two can coexist