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

Discussion in 'C++' started by m0shbear, Mar 23, 2011.

  1. m0shbear

    m0shbear Guest

    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, Mar 23, 2011
    #1
    1. Advertising

  2. m0shbear

    m0shbear Guest

    On Mar 22, 10:51 pm, m0shbear <> 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.
    m0shbear, Mar 23, 2011
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page