For example, in:
virtual auto create_obj() -> std::unique_ptr<Base>
{
return std::unique_ptr<Base>{};
}
What does -> signify ? Since the return type is specified as auto, why is it necessary ?
For example, in:
virtual auto create_obj() -> std::unique_ptr<Base>
{
return std::unique_ptr<Base>{};
}
What does -> signify ? Since the return type is specified as auto, why is it necessary ?
It is referred to as trailing return type, it is simply another way to specify the return type of a function.
One situation where it is useful is returning a function pointer from a function. Here is the "standard" syntax:
void yoyo(){
std::cout << "yoyo!\n";
}
void(*my_fn())(){
return yoyo;
}
Versus the more "modern" syntax:
auto my_fn() -> void(*)(){
return yoyo;
}
Which is much easier to read.
P.S. The trailing return type can be used for function pointer declarations too:
auto yoyo(){ std::cout << "yoyo!\n"; }
auto yoyo_fn() -> void(*)(){ return yoyo; }
auto too_far() -> auto(*)() -> void(*)(){ return yoyo_fn; }
Which is pretty contrived, but is much easier to read than if I had to write in the traditional syntax!