14

Reading the answer for one exercise in C++ Primer, 5th Edition, I found this code:

#ifndef CP5_ex7_04_h #define CP5_ex7_04_h #include <string> class Person { std::string name; std::string address; public: auto get_name() const -> std::string const& { return name; } auto get_addr() const -> std::string const& { return address; } }; #endif 

What does

const -> std::string const& 

mean in this case?

8
  • 4
    Trailing return type syntax. Basically, auto func() -> type = type func(), but the first one has more features. In your case it has no advantages and was probably used because someone who wrote the code likes it more than the normal syntax. Commented Aug 23, 2016 at 15:06
  • Means it is a get only property. Commented Aug 23, 2016 at 15:22
  • 7
    @FirstStep: C++ doesn't have "properties". Commented Aug 23, 2016 at 15:25
  • 2
    @Destructor See this: stackoverflow.com/questions/38541155/… Commented Aug 23, 2016 at 16:00
  • 4
    @FirstStep: C++ doesn't have "methods". Commented Aug 23, 2016 at 16:58

4 Answers 4

21

auto get_name() const -> std::string const& { return name; } is trailing return type notation for the equivalent

std::string const& get_name() const { return name; }

Note that the equivalence is exact in the sense that you can declare a function using one syntax and define it with the other.

(This has been part of the C++ standard since and including C++11).

Sign up to request clarification or add additional context in comments.

14 Comments

@MarkRansom: C++11. You use it whenever you write a lambda (though you probably omit the trailing return type in practice a lot of the time). You'll also see the monstrosity that is auto main() -> int {} wherever Alf writes a C++ answer on SO ;)
@LightnessRacesinOrbit I know, but 1) I honestly didn't know until you told me, and 2) I don't like editing other people's answers, it seems kind of rude. P.S. 3) it's always good to get people to think about leaving better answers.
@MarkRansom: You could have just said "thanks for the info" :P
@user: No, the C++03 syntax already permits std::string const& get_name() const; The purpose of the new syntax is to have more names in lexical scope while the return type is processed, which is handy when using decltype.
@Destructor: In C++11, the feature doesn't exist for functions. In C++14, it doesn't seem to want to work with main, and off the top of my head I'm not sure why that is (I'd be tempted to say it's actually a compiler bug, but neither brand new clang nor brand new GCC are happy with it; I have asked SO for advice). Obviously if you omitted return 0 like a good boy then it would all be moot anyway ;)
|
9

The part -> std::string const& is trailing return type and is new syntax since C++11.

The first const says it a const member function. It can be safely called on a const object of type Person.

The second part simply tells what the return type is - std:string const&.

It is useful when the return type needs to be deduced from a template argument. For known return types, it is no more useful than than using:

std::string const& get_name() const { return name; } 

Comments

4

It all makes more sense when you see an example where it actually matters; as written in the question, it's just an alternative way to declare the return type.

If you have a template function where you can not know the return type in advance, this can actually help. For example:

template <class X, class Y> auto DoSomeThing(X x, Y y) -> decltype(x * y); 

You don't know what types X and Y actually are but you know the return value will have the same type as x * y which can be deduced in this way.

Comments

3

const is a cv-qualifier of the usual kind for the member function : *this is const inside the function.

-> std::string const& pairs with auto to form a trailing return type (see (2)). The difference is only syntactic here -- the following syntax is equivalent:

std::string const& get_name() const { return name; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.