Usage of auto in C++11

The difference is that in the first case auto is deduced to int* while in the second case auto is deduced to int, which results in both p1 and p2 being of type int*.
The type deduction mechanism for auto is equivalent to that of template arguments. The type deduction in your example is therefore similar to

template<typename T>
void foo(T p1);

template<typename T>
void bar(T* p2);

int main()
{
    int i;
    foo(&i);
    bar(&i);
}

where both functions are instantiated as type void(int*) but in the first case T is deduced to int* while in the second case T has type int.

Leave a Comment