Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- class MyCls
- {
- public:
- MyCls()
- {
- std::cout << "MyCls(): " << this << std::endl;
- }
- MyCls(const MyCls& v)
- {
- std::cout << "MyCls(MyCls&& v): " << this << ", other: " << &v << std::endl;
- }
- MyCls(MyCls&& v) noexcept
- {
- std::cout << "MyCls(MyCls&& v): " << this << ", other: " << (&v) << std::endl;
- }
- virtual ~MyCls()
- {
- std::cout << "~MyCls(): " << this << std::endl;
- }
- };
- // Функция возвращает лямбду
- auto makeWalrus(const MyCls& t)
- {
- // Захват переменной name по ссылке и возврат лямбды
- return [=]() {
- std::cout << "I am a walrus, my name is " << &t << '\n'; // неопределённое поведение
- };
- }
- int main()
- {
- // Создаём новый объект с именем Roofus
- // sayName является лямбдой, возвращаемой функцией makeWalrus()
- auto sayName{ makeWalrus(MyCls()) };
- // Вызов лямбды, которую возвращает функция makeWalrus()
- sayName();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement