-2

Note this is NOT a duplicate of What does -> mean in C++?

The question is specific to C++11; where a function can look like:

struct string_accumulator {
}


inline auto collect() -> string_accumulator
{
    return string_accumulator();
}

What is the meaning of the -> in this context?

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
UKMonkey
  • 6,941
  • 3
  • 21
  • 30
  • I didn't downwote, but I think your title could be more specific, like: "what(...) in function declaration?" – R2RT Sep 21 '17 at 09:46

1 Answers1

5

It's a trailing return type. It can be used to explicitly specify return types for lambda expressions or to specify return types that depend on a function's arugments. Examples:

[](int) -> float { return 0.f; };

template <typename A, typename B>
auto foo(A a, B b) -> decltype(a + b) { return a + b; } 
Caleth
  • 52,200
  • 2
  • 44
  • 75
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • It can also be a bit useful just to put the return type in the scope of the function. Like instead of `MyNS::MyClass::MyNestedType MyNS::MyClass::f() { return {}; }`, you can do `auto MyNS::MyClass::f() -> MyNestedType { return {}; }` – aschepler Sep 21 '17 at 09:48