[英]C++ overloading by functor param count type
I am working on "LINQ to Objects" library for C++11. 我正在为C ++ 11开发“LINQ to Objects”库。 I would like to do smth like this:
我想像这样做:
// filtering elements by their value
arr.where( [](double d){ return d < 0; } )
// filtering elements by their value and position
arr.where( [](double d, int i){ return i%2==0; } )
I down want to write arr.where_i( ... )
- it's ugly. 我想写
arr.where_i( ... )
- 这很难看。 So i need function/method overloading by lambda-type... 所以我需要lambda类型的函数/方法重载...
This is my solution: 这是我的解决方案:
template<typename F>
auto my_magic_func(F f) -> decltype(f(1))
{
return f(1);
}
template<typename F>
auto my_magic_func(F f, void * fake = NULL) -> decltype(f(2,3))
{
return f(2,3);
}
int main()
{
auto x1 = my_magic_func([](int a){ return a+100; });
auto x2 = my_magic_func([](int a, int b){ return a*b; });
// x1 == 1+100
// x2 == 2*3
}
Is it SFINAE solution? 是SFINAE解决方案吗? What can you suggest me?
你有什么建议我的?
Maybe something variadic: 也许是可变的东西:
#include <utility>
template <typename F, typename ...Args>
decltype(f(std::declval<Args>()...) my_magic_func(F f, Args &&... args)
{
return f(std::forward<Args>(args)...);
}
Edit: You can also use typename std::result_of<F(Args...)>::type
for the return type, which does the same thing. 编辑:您还可以使用
typename std::result_of<F(Args...)>::type
作为返回类型,它执行相同的操作。
You certainly want SFINAE in your solution. 您当然希望SFINAE在您的解决方案中。 Generally speaking, the result would look something like:
一般来说,结果看起来像:
template<
typename Functor
, typename std::enable_if<
special_test<Functor>::value
, int
>::type = 0
>
return_type
my_magic_func(Functor f);
template<
typename Functor
, typename std::enable_if<
!special_test<Functor>::value
, int
>::type = 0
>
return_type
my_magic_func(Functor f);
such that only one overload would be active at any one time -- all that remains now is carefully crafting that special_test
to have the behaviour we want. 这样在任何时候只有一个重载是活动的 - 现在剩下的就是精心设计
special_test
以获得我们想要的行为。 This is a careful balancing act as you don't want the test to be too specific; 这是一个谨慎的平衡行为,因为您不希望测试过于具体; otherwise we lose generality.
否则我们会失去普遍性。 Quite a shame when writing generic code.
编写通用代码时非常遗憾。 You haven't given too much information (eg are you strictly interested in support for lambdas? monomorphic functors? polymorphic functors?), but I will assume for now that we have access to a
value_type
alias which would correspond to double
in your example. 你没有给出太多的信息(例如你是否对lambdas?单态函子?多态函子?)有严格的兴趣,但我现在假设我们可以访问一个
value_type
别名,它对应于你的例子中的double
。
As such, here's an example condition that will check that a given type is Callable (that's a Standard concept) with signature bool(value_type)
; 因此,这是一个示例条件,它将使用签名
bool(value_type)
检查给定类型是否可调用(这是标准概念bool(value_type)
; ie that it's a predicate of sorts: 即它是各种类型的谓词:
template<typename Functor, typename ValueType>
struct is_unary_predicate {
typedef char (&accepted)[1];
typedef char (&refused)[2];
void consume(bool);
template<
typename X
, typename Y
, typename = decltype( consume(std::declval<X>()(std::declval<Y>())) )
>
accepted
test(X&&, Y&&);
refused test(...);
static constexpr bool value =
sizeof test(std::declval<Functor>(), std::declval<ValueType>())
== sizeof(accepted);
};
Personally I have an is_callable<F, Signature>
trait so that I would only need to write something like template<typename Functor, typename ValueType> using is_unary_predicate = is_callable<Functor, bool(ValueType)>;
我个人有一个
is_callable<F, Signature>
trait,所以我只需要template<typename Functor, typename ValueType> using is_unary_predicate = is_callable<Functor, bool(ValueType)>;
写一些类似template<typename Functor, typename ValueType> using is_unary_predicate = is_callable<Functor, bool(ValueType)>;
(and similarly I could have an is_binary_predicate
alias instead of letting the second overload of my_magic_func
be a catch-all). (同样我可以有一个
is_binary_predicate
别名,而不是让my_magic_func
的第二次重载成为一个全能的)。 Perhaps you'd want to use a similar trait for future uses of SFINAE (although it may be somewhat painful to write without variadic templates). 也许你想在SFINAE的未来使用中使用类似的特性(尽管在没有可变参数模板的情况下编写可能有点痛苦)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.