Почему рекурсивная лямбда с выведенным типом возврата не работает?C++

Программы на C++. Форум разработчиков
Anonymous
Почему рекурсивная лямбда с выведенным типом возврата не работает?

Сообщение Anonymous »

Код: Выделить всё

int main() {
auto fn = [](this auto&& self, T n) {
if (n > 0) {
self(n - 1);
}
};

fn(3);
}
Скомпилировано с Clang -20 -Std = c ++ 23 , но получил следующие ошибки:

Код: Выделить всё

:4:13: error: function 'operator()' with deduced return type cannot be used before it is defined
4 |             self(n - 1);
|             ^
:8:7: note: in instantiation of function template specialization 'main()::(anonymous class)::operator()' requested here
8 |     fn(3);
|       ^
:2:15: note: 'operator()' declared here
2 |     auto fn = [](this auto&& self, T n) {
|               ^
1 error generated.
Compiler returned: 1
См. https://godbolt.org/z/pybm8w9rv
Почему рекурсивный лямбда с параметрами шаблона и аргументом Decucing (this) не работает?>

Подробнее здесь: https://stackoverflow.com/questions/796 ... e-not-work

Вернуться в «C++»