c++ - Why we have to specify data type again after the arrow symbol ( -> ) -


auto can deduce return type why need trailing arrow symbol (->) deduce return type

#include <iostream>    auto add (int i, int j)->int {     return i+j; } int main() {     int x=10,y=20;     std::cout<<add(x,y);  } 

in c++11, there no return type deduction functions. auto not placeholder type deduced here. meaning overloaded.

for functions, auto means return type specified trailing return type. cannot omit trailing return, or program ill-formed.

this feature added language allow return type specification depend on functions parameters, or enclosing class members. considered "seen" time trailing return type reached.

for instance, in class:

namespace baz {     struct foo {         enum bar {something};         bar func();     }; } 

if implement member function out of line in c++03, have this:

baz::foo::bar baz::foo::func() {     return something; } 

we must specify qualified name return type. can become unreadable. trailing return types:

auto baz::foo::func() -> bar {     return something; } 

the full enclosing namespace seen, , bar can specified using unqualified id.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -