capture by references more (the objects live long enough to be safe)

master
Daniel Kolesa 2016-04-27 00:29:55 +01:00
parent a9bbccb1fb
commit 288526e7db
2 changed files with 32 additions and 30 deletions

View File

@ -30,7 +30,7 @@ inline R partition(R range, U pred) {
}
template<typename F> inline auto partition(F func) {
return [f = move(func)](auto obj) { return partition(obj, f); };
return [&func](auto &obj) mutable { return partition(obj, func); };
}
template<typename R, typename P>
@ -42,7 +42,9 @@ inline bool is_partitioned(R range, P pred) {
}
template<typename F> inline bool is_partitioned(F func) {
return [f = move(func)](auto obj) { return is_partitioned(obj, f); };
return [&func](auto &obj) mutable {
return is_partitioned(obj, move(func));
};
}
/* sorting */
@ -256,10 +258,10 @@ inline F for_each(R range, F func) {
}
template<typename F> inline auto for_each(F func) {
return [f = move(func)](auto obj) {
return [&func](auto &obj) mutable {
for (; !obj.empty(); obj.pop_front())
f(obj.front());
return move(f);
func(obj.front());
return move(func);
};
}
@ -271,7 +273,7 @@ inline bool all_of(R range, P pred) {
}
template<typename F> inline auto all_of(F func) {
return [f = move(func)](auto obj) { return all_of(obj, f); };
return [&func](auto &obj) mutable { return all_of(obj, move(func)); };
}
template<typename R, typename P>
@ -282,7 +284,7 @@ inline bool any_of(R range, P pred) {
}
template<typename F> inline auto any_of(F func) {
return [f = move(func)](auto obj) { return any_of(obj, f); };
return [&func](auto &obj) mutable { return any_of(obj, move(func)); };
}
template<typename R, typename P>
@ -293,7 +295,7 @@ inline bool none_of(R range, P pred) {
}
template<typename F> inline auto none_of(F func) {
return [f = move(func)](auto obj) { return none_of(obj, f); };
return [&func](auto &obj) mutable { return none_of(obj, move(func)); };
}
template<typename R, typename T>
@ -305,7 +307,7 @@ inline R find(R range, const T &v) {
}
template<typename T> inline auto find(const T &v) {
return [v](auto obj) { return find(obj, v); };
return [&v](auto &obj) { return find(obj, v); };
}
template<typename R, typename T>
@ -323,7 +325,7 @@ inline R find_last(R range, const T &v) {
}
template<typename T> inline auto find_last(const T &v) {
return [v](auto obj) { return find_last(obj, v); };
return [&v](auto &obj) { return find_last(obj, v); };
}
template<typename R, typename P>
@ -335,7 +337,7 @@ inline R find_if(R range, P pred) {
}
template<typename F> inline auto find_if(F func) {
return [f = move(func)](auto obj) { return find_if(obj, f); };
return [&func](auto &obj) mutable { return find_if(obj, move(func)); };
}
template<typename R, typename P>
@ -347,7 +349,7 @@ inline R find_if_not(R range, P pred) {
}
template<typename F> inline auto find_if_not(F func) {
return [f = move(func)](auto obj) { return find_if_not(obj, f); };
return [&func](auto &obj) mutable { return find_if_not(obj, move(func)); };
}
template<typename R1, typename R2, typename C>
@ -378,7 +380,7 @@ inline RangeSize<R> count(R range, const T &v) {
}
template<typename T> inline auto count(const T &v) {
return [v](auto obj) { return count(obj, v); };
return [&v](auto &obj) { return count(obj, v); };
}
template<typename R, typename P>
@ -391,7 +393,7 @@ inline RangeSize<R> count_if(R range, P pred) {
}
template<typename F> inline auto count_if(F func) {
return [f = move(func)](auto obj) { return count_if(obj, f); };
return [&func](auto &obj) mutable { return count_if(obj, move(func)); };
}
template<typename R, typename P>
@ -404,7 +406,7 @@ inline RangeSize<R> count_if_not(R range, P pred) {
}
template<typename F> inline auto count_if_not(F func) {
return [f = move(func)](auto obj) { return count_if_not(obj, f); };
return [&func](auto &obj) mutable { return count_if_not(obj, move(func)); };
}
template<typename R>
@ -418,7 +420,7 @@ inline bool equal(R range1, R range2) {
}
template<typename R> inline bool equal(R range) {
return [r = move(range)](auto obj) { return equal(obj, r); };
return [&range](auto &obj) mutable { return equal(obj, move(range)); };
}
template<typename R>
@ -427,7 +429,7 @@ R slice_until(R range1, R range2) {
}
template<typename R> inline auto slice_until(R range) {
return [r = move(range)](auto obj) { return slice_until(obj, r); };
return [&range](auto &obj) mutable { return slice_until(obj, move(range)); };
}
/* algos that modify ranges or work with output ranges */
@ -521,7 +523,7 @@ inline T foldl(R range, T init, F func) {
}
template<typename T> inline auto foldl(T init) {
return [v = move(init)](auto obj) { return foldl(obj, v); };
return [&init](auto &obj) mutable { return foldl(obj, move(init)); };
}
template<typename R, typename T>
@ -539,7 +541,7 @@ inline T foldr(R range, T init, F func) {
}
template<typename T> inline auto foldr(T init) {
return [v = move(init)](auto obj) { return foldr(obj, v); };
return [&init](auto &obj) mutable { return foldr(obj, move(init)); };
}
template<typename T, typename F, typename R>
@ -631,7 +633,7 @@ inline MapRange<R, F, detail::MapReturnType<R, F>> map(R range, F func) {
}
template<typename F> inline auto map(F func) {
return [f = move(func)](auto obj) { return map(obj, f); };
return [&func](auto &obj) mutable { return map(obj, move(func)); };
}
template<typename T, typename F>
@ -704,7 +706,7 @@ inline FilterRange<R, detail::FilterPred<R, P>> filter(R range, P pred) {
}
template<typename F> inline auto filter(F func) {
return [f = move(func)](auto obj) { return filter(obj, f); };
return [&func](auto &obj) mutable { return filter(obj, move(func)); };
}
} /* namespace ostd */

View File

@ -485,12 +485,12 @@ template<typename B, typename C, typename V, typename R = V &,
}
template<typename R1, typename ...RR>
JoinRange<B, R1, RR...> join(R1 r1, RR ...rr) {
JoinRange<B, R1, RR...> join(R1 r1, RR ...rr) const {
return JoinRange<B, R1, RR...>(iter(), move(r1), move(rr)...);
}
template<typename R1, typename ...RR>
ZipRange<B, R1, RR...> zip(R1 r1, RR ...rr) {
ZipRange<B, R1, RR...> zip(R1 r1, RR ...rr) const {
return ZipRange<B, R1, RR...>(iter(), move(r1), move(rr)...);
}
@ -585,36 +585,36 @@ inline auto operator|(const R &range, F &&func) -> decltype(func(range)) {
}
inline auto reverse() {
return [](auto obj) { return obj.reverse(); };
return [](auto &obj) { return obj.reverse(); };
}
inline auto movable() {
return [](auto obj) { return obj.movable(); };
return [](auto &obj) { return obj.movable(); };
}
inline auto enumerate() {
return [](auto obj) { return obj.enumerate(); };
return [](auto &obj) { return obj.enumerate(); };
}
template<typename T>
inline auto take(T n) {
return [n](auto obj) { return obj.take(n); };
return [n](auto &obj) { return obj.take(n); };
}
template<typename T>
inline auto chunks(T n) {
return [n](auto obj) { return obj.chunks(n); };
return [n](auto &obj) { return obj.chunks(n); };
}
/* TODO: can't use generalized captures on packs so gotta workaround */
template<typename R1, typename ...R>
inline auto join(R1 r1, R ...rr) {
return [=](auto obj) mutable { return obj.join(move(r1), move(rr)...); };
return [=](auto &obj) mutable { return obj.join(move(r1), move(rr)...); };
}
template<typename R1, typename ...R>
inline auto zip(R1 r1, R ...rr) {
return [=](auto obj) mutable { return obj.zip(move(r1), move(rr)...); };
return [=](auto &obj) mutable { return obj.zip(move(r1), move(rr)...); };
}
template<typename T>