dimanche 26 février 2017

What is this use of auto ? - ADL?

I have always thought that auto should be used in the following form auto varName = someOtherVar;. Today I have found that I could also use auto varName(someOtherVar);. At first I thought that maybe this is Argument Depending Lookup at work. But I am not sure. What are the restrictions for using such auto syntax? Below is some code:

#include <iostream>
#include <string>
#include <vector>

class Person {
    public:
    Person(std::string s) : name(s) {}
    Person(const Person& p) : name(p.name) {}
    std::string name;
};

int main()
{
    Person p("hello");
    auto p2(p); // equivalent to auto p2 = p; ?
    std::cout << p2.name;
}

Aucun commentaire:

Enregistrer un commentaire