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

M

m0shbear

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?
 
M

m0shbear

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top