Программы на C++. Форум разработчиков
-
Anonymous
Подсчет количества существующих значений в массиве опционов с помощью std ::
Сообщение
Anonymous »
My Colleague Ports A C ++ программы с диапазонами на macOS и наблюдает неожиданную ошибку компиляции. >
Код: Выделить всё
#include
#include
int main() {
std::optional ops[4];
//...
return (int)std::ranges::count_if( ops, &std::optional::has_value );
};
< /code>
GCC и MSVC в порядке с программой, но Clang отображает длинную ошибку: < /p>
error: no matching function for call to object of type 'const __count_if::__fn'
7 | return (int)std::ranges::count_if( ops, &std::optional::has_value );
| ^~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/clang-19.1.0/bin/../include/c++/v1/__algorithm/ranges_count_if.h:62:3: note: candidate template ignored: constraints not satisfied [with _Range = std::optional (&)[4], _Proj = identity, _Predicate = bool (std::__optional_storage_base::*)() const noexcept]
62 | operator()(_Range&& __r, _Predicate __pred, _Proj __proj = {}) const {
| ^
/opt/compiler-explorer/clang-19.1.0/bin/../include/c++/v1/__algorithm/ranges_count_if.h:60:13: note: because 'indirect_unary_predicate' evaluated to false
60 | indirect_unary_predicate _Predicate>
| ^
/opt/compiler-explorer/clang-19.1.0/bin/../include/c++/v1/__iterator/concepts.h:191:60: note: because 'predicate' evaluated to false
191 | indirectly_readable && copy_constructible && predicate &&
| ^
/opt/compiler-explorer/clang-19.1.0/bin/../include/c++/v1/__concepts/predicate.h:28:21: note: because 'regular_invocable' evaluated to false
28 | concept predicate = regular_invocable && __boolean_testable;
| ^
/opt/compiler-explorer/clang-19.1.0/bin/../include/c++/v1/__concepts/invocable.h:34:29: note: because 'invocable' evaluated to false
34 | concept regular_invocable = invocable;
| ^
/opt/compiler-explorer/clang-19.1.0/bin/../include/c++/v1/__concepts/invocable.h:28:3: note: because 'std::invoke(std::forward(__fn), std::forward(__args)...)' would be invalid: no matching function for call to 'invoke'
28 | std::invoke(std::forward(__fn), std::forward(__args)...); // not required to be equality preserving
| ^
/opt/compiler-explorer/clang-19.1.0/bin/../include/c++/v1/__algorithm/ranges_count_if.h:54:3: note: candidate function template not viable: requires at least 3 arguments, but 2 were provided
54 | operator()(_Iter __first, _Sent __last, _Predicate __pred, _Proj __proj = {}) const {
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Демонстрация онлайн:
https://gcc.godbolt.org/z/no55zpzgz
Я не понимаю, что не так с программой? /п>
Подробнее здесь:
https://stackoverflow.com/questions/794 ... -stdranges
1738963063
Anonymous
My Colleague Ports A C ++ программы с диапазонами на macOS и наблюдает неожиданную ошибку компиляции. >
[code]#include
#include
int main() {
std::optional ops[4];
//...
return (int)std::ranges::count_if( ops, &std::optional::has_value );
};
< /code>
GCC и MSVC в порядке с программой, но Clang отображает длинную ошибку: < /p>
error: no matching function for call to object of type 'const __count_if::__fn'
7 | return (int)std::ranges::count_if( ops, &std::optional::has_value );
| ^~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/clang-19.1.0/bin/../include/c++/v1/__algorithm/ranges_count_if.h:62:3: note: candidate template ignored: constraints not satisfied [with _Range = std::optional (&)[4], _Proj = identity, _Predicate = bool (std::__optional_storage_base::*)() const noexcept]
62 | operator()(_Range&& __r, _Predicate __pred, _Proj __proj = {}) const {
| ^
/opt/compiler-explorer/clang-19.1.0/bin/../include/c++/v1/__algorithm/ranges_count_if.h:60:13: note: because 'indirect_unary_predicate' evaluated to false
60 | indirect_unary_predicate _Predicate>
| ^
/opt/compiler-explorer/clang-19.1.0/bin/../include/c++/v1/__iterator/concepts.h:191:60: note: because 'predicate' evaluated to false
191 | indirectly_readable && copy_constructible && predicate &&
| ^
/opt/compiler-explorer/clang-19.1.0/bin/../include/c++/v1/__concepts/predicate.h:28:21: note: because 'regular_invocable' evaluated to false
28 | concept predicate = regular_invocable && __boolean_testable;
| ^
/opt/compiler-explorer/clang-19.1.0/bin/../include/c++/v1/__concepts/invocable.h:34:29: note: because 'invocable' evaluated to false
34 | concept regular_invocable = invocable;
| ^
/opt/compiler-explorer/clang-19.1.0/bin/../include/c++/v1/__concepts/invocable.h:28:3: note: because 'std::invoke(std::forward(__fn), std::forward(__args)...)' would be invalid: no matching function for call to 'invoke'
28 | std::invoke(std::forward(__fn), std::forward(__args)...); // not required to be equality preserving
| ^
/opt/compiler-explorer/clang-19.1.0/bin/../include/c++/v1/__algorithm/ranges_count_if.h:54:3: note: candidate function template not viable: requires at least 3 arguments, but 2 were provided
54 | operator()(_Iter __first, _Sent __last, _Predicate __pred, _Proj __proj = {}) const {
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[/code]
Демонстрация онлайн: https://gcc.godbolt.org/z/no55zpzgz
Я не понимаю, что не так с программой? /п>
Подробнее здесь: [url]https://stackoverflow.com/questions/79422181/counting-the-number-of-present-values-in-array-of-optionals-with-stdranges[/url]