Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Discussion: variant of std::search that uses range-based for constructs (http://www.velocityreviews.com/forums/t745569-discussion-variant-of-std-search-that-uses-range-based-for-constructs.html)

m0shbear 03-23-2011 02:51 AM

Discussion: variant of std::search that uses range-based for constructs
 
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?

m0shbear 03-23-2011 02:59 AM

Re: Discussion: variant of std::search that uses range-based for constructs
 
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.


All times are GMT. The time now is 07:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.