C ++ 14: What's the difference between decltype (auto) and "auto" as a return type?

I had a simple program like this:

template<class T1,class T2>
decltype(auto) Add(T1&& t1,T2&& t2)
{
    return t1+t2
}

int main(){
    int i=Add(1,2);
    return 0;
}

      

It compiles with clang ++ -std = C ++ 14. I found that I can exclude the "decltype" keyword like below:

template<class T1,class T2>
auto Add(T1&& t1,T2&& t2)
{
    return t1+t2;
}
int main(){
    int i=Add(1,2);
    return 0;
}

      

However, not a problem. So my question is, when is "decltype (auto)" and "auto" different, in different scenarios? I just think "decltype (auto)" is for a more complex situation where "auto" cannot work? But what is it, please kindly suggest.

Many thanks.

+3


source to share





All Articles