std::any_of and friends without predicates

282 views
Skip to first unread message

Anton Bikineev

unread,
Sep 11, 2018, 6:32:59 AM9/11/18
to ISO C++ Standard - Future Proposals
Today I wanted to check a sequence of convertible-to-bool elements type for being all set to false and found out that there are no counterparts for std::any_of, std::all_of, std::none_of that would take no predicate and just use (*it) in underlying conditions. Would it make sense to add them to avoid writing silly predicates, like
[](const auto& i) noexcept -> bool {return i;}
?
Sorry if the question has popped up before.

Sarfaraz Nawaz

unread,
Sep 11, 2018, 8:53:17 AM9/11/18
to std-pr...@isocpp.org
I think the standard missed that.

Anyway, it seems it'd be good if the core language allows passing `type` as argument to a function, as the compiler should be able to create a callable entity out of this so that the following could be allowed:

 std::any_of(begin(x), end(x), bool);

and from the third argument, the compiler should create a function that takes zero or more arguments (templatized) and returns bool, so the above  should be equivalent to this:

 std::any_of(begin(x), end(x), [(auto && .. args) { return bool{args...}; });

I guess such feature would be very useful in other data transformation scenario as well.

--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/9b1c3d8e-39ef-4a68-a49e-ef8ad13e5e74%40isocpp.org.

Matthew Woehlke

unread,
Sep 11, 2018, 10:39:58 AM9/11/18
to std-pr...@isocpp.org, Sarfaraz Nawaz
On 2018-09-11 08:53, Sarfaraz Nawaz wrote:
> Anyway, it seems it'd be good if the core language allows passing `type` as
> argument to a function, as the compiler should be able to create a callable
> entity out of this so that the following could be allowed:
>
> std::any_of(begin(x), end(x), bool);
>
> and from the third argument, the compiler should create a function that
> takes zero or more arguments (templatized) and returns bool

That seems... overly involved for a language feature of somewhat
questionable value (*as* a language feature).

Wouldn't it be much simpler to have:

std::any_of(begin(x), end(x), std::cast<bool>);

...? And can't we already implement that?

--
Matthew

Sarfaraz Nawaz

unread,
Sep 11, 2018, 11:23:53 AM9/11/18
to ISO C++ Standard - Future Proposals, naw...@gmail.com
Hmm... the idea of std::cast<>  looks good. Seems it's quite easily implementable as well:
    
    template<typename T>
   
constexpr auto cast = [](auto && ... args) {
       
return T(args ... );  // ignored std::forward for brevity
   
};



mutant....@gmail.com

unread,
Sep 12, 2018, 2:40:30 AM9/12/18
to ISO C++ Standard - Future Proposals, naw...@gmail.com
If the type is already convertible to bool then std::any_of(std::begin(x), std::end(x), std::identity); might work?

A cast operator for std::any_of(begin(x), end(x), std::cast<bool>) would probably be nicer though.

Hopefully we get some form of abbreviated lambdas though as that might be best:
std::any_of(begin(x), end(x), ($it) => it);
Reply all
Reply to author
Forward
0 new messages