Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CPlusPlusNotesForProfessionals.pdf
Скачиваний:
47
Добавлен:
20.05.2023
Размер:
5.11 Mб
Скачать

Chapter 99: Trailing return type

Section 99.1: Avoid qualifying a nested type name

class ClassWithAReallyLongName { public:

class Iterator { /* ... */ }; Iterator end();

};

Defining the member end with a trailing return type:

auto ClassWithAReallyLongName::end() -> Iterator { return Iterator(); }

Defining the member end without a trailing return type:

ClassWithAReallyLongName::Iterator ClassWithAReallyLongName::end() { return Iterator(); }

The trailing return type is looked up in the scope of the class, while a leading return type is looked up in the enclosing namespace scope and can therefore require "redundant" qualification.

Section 99.2: Lambda expressions

A lambda can only have a trailing return type; the leading return type syntax is not applicable to lambdas. Note that in many cases it is not necessary to specify a return type for a lambda at all.

struct Base {};

struct Derived1 : Base {}; struct Derived2 : Base {};

auto lambda = [](bool b) -> Base* { if (b) return new Derived1; else return new Derived2; }; // ill-formed: auto lambda = Base* [](bool b) { ... };

GoalKicker.com – C++ Notes for Professionals

508