2017-03-17 87 views
1

我想使用boost::range::combine作为笛卡儿的力量,而不仅仅是一个产品。boost :: range ::结合重复的参数

因此,而不是这样的表达boost::range::combine(myRange, myRange, myRange);写东西像myCombine(myRange, 3);

它是如何实现的?

回答

1

在C++ 17或C++ 14中实现这一点会更容易和更清晰,但由于您用标记了此处,因此这是一个兼容的实现。这里有一种调用函数对象f的通用方法,它具有重复N次的相同参数。

首先,我们需要的结合通用函数对象f的第一个参数,然后接受任何数量的参数的方式:

template <typename TF, typename T> 
struct bound 
{ 
    TF _f; 
    T _x; 

    template <typename TFFwd, typename TFwd> 
    bound(TFFwd&& f, TFwd&& x) 
     : _f{std::forward<TFFwd>(f)}, _x{std::forward<TFwd>(x)} 
    { 
    } 

    template <typename... Ts> 
    auto operator()(Ts&&... xs) 
     -> decltype(_f(_x, std::forward<Ts>(xs)...)) 
    { 
     return _f(_x, std::forward<Ts>(xs)...); 
    } 
}; 

template <typename TF, typename T> 
auto bind_first(TF&& f, T&& x) 
    -> decltype(bound<TF&&, T&&>(std::forward<TF>(f), std::forward<T>(x))) 
{ 
    return bound<TF&&, T&&>(std::forward<TF>(f), std::forward<T>(x)); 
} 

然后,我们需要一个递归helper将结合一个参数x多个TN时间:

template <std::size_t TN> 
struct helper 
{ 
    template <typename TF, typename T> 
    auto operator()(TF&& f, T&& x) 
     -> decltype(helper<TN - 1>{}(bind_first(std::forward<TF>(f), x), x)) 
    { 
     return helper<TN - 1>{}(bind_first(std::forward<TF>(f), x), x); 
    } 
}; 

template <> 
struct helper<0> 
{ 
    template <typename TF, typename T> 
    auto operator()(TF&& f, T&& x) 
     -> decltype(f(x)) 
    { 
     return f(x); 
    } 
}; 

最后,我们可以提供一个很好的界面:

template <std::size_t TN, typename TF, typename T> 
auto call_with_same_arg(TF&& f, T&& x) 
    -> decltype(helper<TN - 1>{}(std::forward<TF>(f), std::forward<T>(x))) 
{ 
    return helper<TN - 1>{}(std::forward<TF>(f), std::forward<T>(x)); 
} 

用法:

int add(int a, int b, int c) 
{ 
    return a + b + c; 
} 

int main() 
{ 
    assert(call_with_same_arg<3>(add, 5) == 15); 
} 

live wandbox example


下面是一个完整的C++ 17的实现同样的事情:

template <std::size_t TN, typename TF, typename T> 
decltype(auto) call_with_same_arg(TF&& f, T&& x) 
{ 
    if constexpr(TN == 1) 
    { 
     return f(x); 
    } 
    else 
    { 
     return call_with_same_arg<TN - 1>(
      [&](auto&&... xs){ return f(x, std::forward<decltype(xs)>(xs)...); }, x); 
    } 
} 

live wandbox example


对于完整性,C++ 14的实现:

template <std::size_t TN> 
struct helper 
{ 
    template <typename TF, typename T> 
    decltype(auto) operator()(TF&& f, T&& x) 
    { 
     return helper<TN - 1>{}(
      [&](auto&&... xs){ return f(x, std::forward<decltype(xs)>(xs)...); }, x); 
    } 
}; 

template <> 
struct helper<0> 
{ 
    template <typename TF, typename T> 
    decltype(auto) operator()(TF&& f, T&& x) 
    { 
     return f(x); 
    } 
}; 

template <std::size_t TN, typename TF, typename T> 
decltype(auto) call_with_same_arg(TF&& f, T&& x) 
{ 
    return helper<TN - 1>{}(std::forward<TF>(f), std::forward<T>(x)); 
} 

live wandbox example

+0

谢谢你的答案。如果你考虑过其他C++标准的解决方案,可以在这里写出来。我只写了C++ 11,以表明它可能(也是)模板元编程技巧。 –

+0

欧,你刚刚发布了C++ 17版本。有没有什么与C++ 14相同的简单解决方案的C + + 17? –

+0

@YaroslavKishchenko:它有点长,但比C++ 11更好。将在一秒后发布。 –