-1

While analyzing some code I found something like this

auto foo() const -> std::string const&

It's a bit confusing for me. What does that mean exactly? To be more precisely I mean about the part:

std::string const&
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Milosz
  • 35
  • 2
  • Do you know how return type deduction works? – πάντα ῥεῖ Oct 06 '22 at 10:24
  • a reference to std::string which text you can not change only read – Alexander Sobetskiy Oct 06 '22 at 10:24
  • 1
    It's a function returning a `const` reference to a `std::string` – thought there wouldn't have been need to apply trailing return type, alternative (classic) variant of: `std::string const& foo() const`. – Aconcagua Oct 06 '22 at 10:25
  • 2
    Not sure where you are confused, but since it wasn't mentioned yet: `const std::string&` and `std::string const &` are exactly the same – Yksisarvinen Oct 06 '22 at 10:26
  • 1
    Look up `trailing return type` in your favorite [c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason Oct 06 '22 at 10:26
  • Some people disregard the tradition of writing `const` on the left, purportedly because it makes types easier to read. – HolyBlackCat Oct 06 '22 at 10:28
  • [Similar question was asked yesterday](https://stackoverflow.com/questions/73961495/what-does-const-in-a-function-template-definition-mean). – Jason Oct 06 '22 at 10:30

1 Answers1

0

It's the return type: a reference to a const string.

Torsten Robitzki
  • 3,041
  • 1
  • 21
  • 35