Незаконное использование лямбда-выражения внутри предложения require?C++

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

Сообщение Anonymous »

Почему все три компилятора отклоняют вызов f2? Нарушаем ли мы какие-либо правила стандарта C++20 при вызове немедленно вызываемого лямбда-выражения вместо функции внутри предложения require?
// note: the function body of `g` is the exact same as the
// lambda body of the requires-clause of function `f2`

constexpr auto g(auto b) {
using c1 = decltype([](int){ return true; });
using c2 = decltype([](bool){ return true; });
struct v : c1, c2 { using c1::operator(), c2::operator(); };
return v{}(decltype(b){});
}
auto f1(auto a) requires (g(decltype(a){})) {}
auto f2(auto a) requires ([](auto b) {
using c1 = decltype([](int){ return true; });
using c2 = decltype([](bool){ return true; });
struct v : c1, c2 { using c1::operator(), c2::operator(); };
return v{}(decltype(b){});
}(decltype(a){})) {}

auto m() {
f1(true); // all ok
f2(true); // all reject
}

Демо

Сообщение об ошибке Clang:
:12:5: error: no matching function for call to 'f2'
12 | f2(true);
| ^~
:9:6: note: candidate template ignored: constraints not satisfied
[with a:auto = bool]
9 | auto f2(auto a) requires ([](auto b){ body }(decltype(a){})){}
| ^
:9:27: note: because substituted constraint expression is ill-formed:
no matching function
for call to object of type '(lambda at :9:27)'
9 | auto f2(auto a) requires ([](auto b){ body }(decltype(a){})){}
| ^

Сообщение об ошибке GCC:
: In instantiation of 'struct::v':
:9:39: required from ' [with auto:4 = bool]'
4 | struct v : c1, c2 { using c1::operator(), c2::operator(); }; \
| ^
:9:45: required by substitution of 'template auto
f2(auto:3) requires ()({}) [with auto:3 = bool]'
9 | auto f2(auto a) requires ([](auto b){ body }(decltype(a){})){}
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
:12:7: required from here
12 | f2(true);
| ~~^~~~~~
:4:44: error: type '::v::' is not a base
type for type '::v'
4 | struct v : c1, c2 { using c1::operator(), c2::operator(); }; \
| ^
:9:39: note: in expansion of macro 'body'
9 | auto f2(auto a) requires ([](auto b){ body }(decltype(a){})){}
| ^~~~
:4:60: error: type '::v::' is not a base
type for type '::v'
4 | struct v : c1, c2 { using c1::operator(), c2::operator(); }; \
| ^
:9:39: note: in expansion of macro 'body'
9 | auto f2(auto a) requires ([](auto b){ body }(decltype(a){})){}
| ^~~~
: In instantiation of ' [with auto:4 = bool]':
:9:45: required by substitution of 'template auto
f2(auto:3) requires ()({}) [with auto:3 = bool]'
9 | auto f2(auto a) requires ([](auto b){ body }(decltype(a){})){}
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
:12:7: required from here
12 | f2(true);
| ~~^~~~~~
:5:15: error: request for member 'operator()' is ambiguous
5 | return v{}(decltype(b){});
| ~~~^~~~~~~~~~~~~~~
:9:39: note: in expansion of macro 'body'
9 | auto f2(auto a) requires ([](auto b){ body }(decltype(a){})){}
| ^~~~
:3:25: note: candidates are: '::'
3 | using c2 = decltype([](bool){ return true; }); \
| ^
:9:39: note: in expansion of macro 'body'
9 | auto f2(auto a) requires ([](auto b){ body }(decltype(a){})){}
| ^~~~
:2:25: note: '::'
2 | using c1 = decltype([](int){ return true; }); \
| ^
:9:39: note: in expansion of macro 'body'
9 | auto f2(auto a) requires ([](auto b){ body }(decltype(a){})){}
| ^~~~
: In function 'auto m()':
:12:7: error: no matching function for call to 'f2(bool)'
12 | f2(true);
| ~~^~~~~~
:9:6: note: candidate: 'template auto f2(auto:3)
requires ()({})'
9 | auto f2(auto a) requires ([](auto b){ body }(decltype(a){})){}
| ^~
:9:6: note: substitution of deduced template arguments resulted
in errors seen above


Подробнее здесь: https://stackoverflow.com/questions/784 ... res-clause
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

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