Skip to content

Commit

Permalink
Merge pull request #62 from TaeZStkyoht/fix_result_of_deprecation
Browse files Browse the repository at this point in the history
Fix result_of deprecation with compatibility
  • Loading branch information
k06a authored Dec 31, 2021
2 parents 55304b9 + 239acc8 commit 0df37ed
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions include/boolinq/boolinq.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,22 @@
#include <set>
#include <unordered_set>

//

// The result_of was deprecated since C++20, so redefine it using invoke_result
#if (defined(_MSC_VER) && _MSVC_LANG >= 202002L) || __cplusplus >= 202002L
namespace std
{
template<typename>
struct result_of;

template<typename _Callable, typename ..._Args>
struct result_of<_Callable(_Args...)>
{
using type = std::invoke_result<_Callable, _Args...>::type;
};
}
#endif
namespace boolinq {

//
namespace priv {
// The result_of was removed since C++20 by not all but some compilers.
// For the sake of compatibility, use own define but in private subspace
// to avoid collisions with std in case of using both std and boolinq.
template<typename _Callable>
struct result_of;

template<typename _Callable, typename... _Args>
struct result_of<_Callable(_Args...)> {
typedef decltype(std::declval<_Callable>()(std::declval<_Args>()...)) type;
};
}

namespace boolinq {
//

struct LinqEndException {};

Expand Down Expand Up @@ -211,7 +207,7 @@ namespace boolinq {
);
}

template<typename F, typename _TRet = typename std::result_of<F(T, int)>::type>
template<typename F, typename _TRet = typename priv::result_of<F(T, int)>::type>
Linq<std::tuple<Linq<S, T>, int>, _TRet> select_i(F apply) const
{
return Linq<std::tuple<Linq<S, T>, int>, _TRet>(
Expand All @@ -225,7 +221,7 @@ namespace boolinq {
);
}

template<typename F, typename _TRet = typename std::result_of<F(T)>::type>
template<typename F, typename _TRet = typename priv::result_of<F(T)>::type>
Linq<std::tuple<Linq<S, T>, int>, _TRet> select(F apply) const
{
return select_i([apply](T value, int /*index*/) { return apply(value); });
Expand Down Expand Up @@ -260,7 +256,7 @@ namespace boolinq {

template<
typename F,
typename _TRet = typename std::result_of<F(T, int)>::type,
typename _TRet = typename priv::result_of<F(T, int)>::type,
typename _TRetVal = typename _TRet::value_type
>
Linq<std::tuple<Linq<S, T>, _TRet, int, bool>, _TRetVal> selectMany_i(F apply) const
Expand Down Expand Up @@ -291,7 +287,7 @@ namespace boolinq {

template<
typename F,
typename _TRet = typename std::result_of<F(T)>::type,
typename _TRet = typename priv::result_of<F(T)>::type,
typename _TRetVal = typename _TRet::value_type
>
Linq<std::tuple<Linq<S, T>, _TRet, int, bool>, _TRetVal> selectMany(F apply) const
Expand All @@ -301,7 +297,7 @@ namespace boolinq {

template<
typename F,
typename _TKey = typename std::result_of<F(T)>::type,
typename _TKey = typename priv::result_of<F(T)>::type,
typename _TValue = Linq<std::tuple<Linq<S, T>, int>, T> // where(predicate)
>
Linq<std::tuple<Linq<S, T>, Linq<S, T>, std::unordered_set<_TKey> >, std::pair<_TKey, _TValue> > groupBy(F apply) const
Expand All @@ -325,7 +321,7 @@ namespace boolinq {
);
}

template<typename F, typename _TRet = typename std::result_of<F(T)>::type>
template<typename F, typename _TRet = typename priv::result_of<F(T)>::type>
Linq<std::tuple<Linq<S, T>, std::unordered_set<_TRet> >, T> distinct(F transform) const
{
return Linq<std::tuple<Linq<S, T>, std::unordered_set<_TRet> >, T>(
Expand Down Expand Up @@ -418,7 +414,7 @@ namespace boolinq {
return start;
}

template<typename F, typename _TRet = typename std::result_of<F(T)>::type>
template<typename F, typename _TRet = typename priv::result_of<F(T)>::type>
_TRet sum(F transform) const
{
return aggregate<_TRet>(_TRet(), [transform](_TRet accumulator, T value) {
Expand All @@ -432,7 +428,7 @@ namespace boolinq {
return sum([](T value) { return TRet(value); });
}

template<typename F, typename _TRet = typename std::result_of<F(T)>::type>
template<typename F, typename _TRet = typename priv::result_of<F(T)>::type>
_TRet avg(F transform) const
{
int count = 0;
Expand Down

0 comments on commit 0df37ed

Please sign in to comment.