Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Discussion: variant of std::search that uses range-based for constructs

Reply
Thread Tools

Discussion: variant of std::search that uses range-based for constructs

 
 
m0shbear
Guest
Posts: n/a
 
      03-23-2011
I am using the following variant of std::search to simplify my code.


template <class ForwardIterable1, class ForwardIterable2>
auto search(ForwardIterable1&& _1, ForwardIterable2&& _2) ->
decltype(_1.begin()) {
return std::search(_1.begin(), _1.end(), _2.begin(),
_2.end());
}

template <class ForwardIterable1, class ForwardIterable2, class
BinaryPredicate>
auto search(ForwardIterable1&& _1, ForwardIterable2&& _2,
BinaryPredicate _p) -> decltype(_1.begin()) {
return std::search(_1.begin(), _2.begin(), _p);
}

Two immediate questions arise:
1) Is '&&' appropriate to use here or should 'const&' be used instead?
2) Are there plans for this to be in the final c++0x (yes i know this
question is better-suited to be asked on comp.std.c++) or will this
probably a compiler extension which will only be useful after posting
as a bugreport to $COMPILERVENDOR?
 
Reply With Quote
 
 
 
 
m0shbear
Guest
Posts: n/a
 
      03-23-2011
On Mar 22, 10:51*pm, m0shbear <andrey....@gmail.com> wrote:
> I am using the following variant of std::search to simplify my code.
>
> template <class ForwardIterable1, class ForwardIterable2>
> auto search(ForwardIterable1&& _1, ForwardIterable2&& _2) ->
> decltype(_1.begin()) {
> * * * * return std::search(_1.begin(), _1.end(), _2.begin(),
> _2.end());
>
> }
>
> template <class ForwardIterable1, class ForwardIterable2, class
> BinaryPredicate>
> auto search(ForwardIterable1&& _1, ForwardIterable2&& _2,
> BinaryPredicate _p) -> decltype(_1.begin()) {
> * * * * return std::search(_1.begin(), _2.begin(), _p);
>
> }
>
> Two immediate questions arise:
> 1) Is '&&' appropriate to use here or should 'const&' be used instead?
> 2) Are there plans for this to be in the final c++0x (yes i know this
> question is better-suited to be asked on comp.std.c++) or will this
> probably a compiler extension which will only be useful after posting
> as a bugreport to $COMPILERVENDOR?


Corrections: change decltype(_1.begin()) to decltype(std::begin(_1))
change _1.begin() with std::begin(_1); likewise for _1.end() and for
_2.

Updated code should be:
template <class ForwardIterable1, class ForwardIterable2>
auto search(ForwardIterable1&& _1, ForwardIterable2&& _2) ->
decltype(std::begin(_1)) {
return std::search(std::begin(_1), std::end(_1),
std::begin(_2), std::end(_2));
}

template <class ForwardIterable1, class ForwardIterable2, class
BinaryPredicate>
auto search(ForwardIterable1&& _1, ForwardIterable2&& _2,
BinaryPredicate _p) -> decltype(std::begin(_1)) {
return std::search(std::begin(_1), std::end(_1),
std::begin(_2), std::end(_2), p);
}

I forgot that the N2930 iterator used the format of std::begin/end
instead of class-wrapping begin and end.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
<% ... %> constructs? Martin Schmid ASP .Net 2 09-30-2006 06:33 AM
70631 Mining the Web: Jacobian Matrix Constructs with eigenVectorSearching 70631 Web Science ASP .Net 0 11-16-2004 10:01 PM
24628 Mining the Web: Jacobian Matrix Constructs with eigenVectorSearching 24628 Web Science Cisco 0 11-16-2004 10:01 PM
Server tags cannot contain <% ... %> constructs Marcos MOS ASP .Net 5 04-15-2004 07:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57