Initializer List as parameter to any_of, none_of, all_of, etc

379 views
Skip to first unread message

push...@gmail.com

unread,
Feb 25, 2016, 6:19:09 AM2/25/16
to ISO C++ Standard - Future Proposals
It would be great to have functions any_of, none_of, all_of what works with initializer lists

now it is:

 int a = 0, b = 0, c = 1;
 std
::initializer_list<int> arr = {a,b,c};
 
if (std::any_of(std::begin(arr), std::end(arr), [](int x){return x > 0;}))
     std
::cout<< "any > 0" <<std::endl;

proposal is:

 int a = 0, b = 0, c = 1;
 
if (std::any_of({a,b,c}, [](int x){return x > 0;}))
     std
::cout<< "any > 0" <<std::endl;

Daniel Krügler

unread,
Feb 25, 2016, 6:43:46 AM2/25/16
to std-pr...@isocpp.org
This feature would automatically fall out of the range TS,

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4569.pdf

because an std::initializer_list meets the requirements for the
InputRange concept (See the any_of declaration accepting ranges on
page 122).

- Daniel

T. C.

unread,
Feb 28, 2016, 4:44:27 AM2/28/16
to ISO C++ Standard - Future Proposals


On Thursday, February 25, 2016 at 7:43:46 PM UTC+8, Daniel Krügler wrote:
2016-02-25 12:19 GMT+01:00  <push...@gmail.com>:
> It would be great to have functions any_of, none_of, all_of what works with
> initializer lists
>
> now it is:
>
>  int a = 0, b = 0, c = 1;
>  std::initializer_list<int> arr = {a,b,c};
>  if (std::any_of(std::begin(arr), std::end(arr), [](int x){return x > 0;}))
>      std::cout<< "any > 0" <<std::endl;
>
> proposal is:
>
>  int a = 0, b = 0, c = 1;
>  if (std::any_of({a,b,c}, [](int x){return x > 0;}))
>      std::cout<< "any > 0" <<std::endl;

This feature would automatically fall out of the range TS, 
 
Not the one in OP's code, though, because you can't deduce std::initializer_list out of {a, b, c}. For that to work, you'll need an overload taking a std::initializer_list<T>.

I'm not sure providing such an overload is a good idea, though, since constructing the backing array is a lot of copying.

Daniel Krügler

unread,
Feb 28, 2016, 1:57:47 PM2/28/16
to std-pr...@isocpp.org
2016-02-28 10:44 GMT+01:00 T. C. <rs2...@gmail.com>:
> Not the one in OP's code, though, because you can't deduce
> std::initializer_list out of {a, b, c}. For that to work, you'll need an
> overload taking a std::initializer_list<T>.

Agreed, I didn't look careful enough at the OP's code.

- Daniel
Reply all
Reply to author
Forward
0 new messages