Why should we again indicate the data type after the arrow character (->)

auto can output the type of the return value, then why should we loop the arrow character (->) to output the 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);
 }
+4
source share
2 answers

In C ++ 11, there are no return type residues for functions. autois not the type of placeholder that should be inferred here. You can say that its meaning is overloaded.

For functions, autosimply means that the return type will be set as the return type. You cannot omit the final return, or your program will be poorly formed.

​​ , . "" .

, :

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

- ++ 03, :

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

. . :

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

, bar .

+7

, ++ 11 auto, , , . auto ++ 14.

+1

Source: https://habr.com/ru/post/1685398/


All Articles