【发布时间】:2017-09-25 09:21:39
【问题描述】:
堆栈溢出的某个人编写了一种有趣的方法来将 lambda 或仿函数捕获到您自己的类中。我试图简化它,我想我已经接近但遇到了一些麻烦。他们的例子是:
// OT => Object Type
// RT => Return Type
// A ... => Arguments
template<typename OT, typename RT, typename ... A>
struct lambda_expression {
OT _object;
RT(OT::*_function)(A...)const; // A pointer to a member function,
// specifically the operator()
lambda_expression(const OT & object) // Constructor
: _object(object),
_function(&decltype(_object)::operator()) {} // Assigning the function pointer
RT operator() (A ... args) const {
return (_object.*_function)(args...);
}
};
基本上这可以让你去:
int captureMe = 2;
auto lambda = [=](int a, int b) { return a + b + captureMe;};
lambda_expression<decltype(lambda), int, int, int>(lambda);
我试图简化这一点,并认为不需要包含在 lambda_expression 类中的指针,因为您可以调用函数对象本身,而不是调用指向 operator() 的指针。所以我尝试了这个:
template <typename OT, typename ... Args> // No Return type specified
struct lambdaContainer
{
lambdaContainer(OT funcObj) : funcObj(funcObj){ }
OT funcObj; // No pointer, just the function object.
auto operator()(Args... args)
{
return funcObj(args...); // Call the function object directly
}
};
然后是这样的:
int captureMe = 2;
auto lambda = [=](int a, int b) { return a + b + captureMe; };
lambdaContainer<decltype(lambda), int, int> lam(lambda);
auto i = lam(1, 1);
// i = 4;
我写这行的地方:
auto operator()(Args... args)
{
return funcObj(args...);
}
显然:
decltype(auto) operator()(Args... args) //works in C++14 apparently.
但我尝试不使用 auto 关键字,但我失败了,我想了解 Args... 是如何工作的。我试过了:
decltype(funObj(Args...) operator()(Args... args) // this failed
decltype(OT(Args...) operator() (Args... args) // this failed
auto operator() (Args... args) -> decltype(funcObj(Args...)) // this failed
auto operator() (Args... args) -> decltype(OT(Args...)) // this failed
如何扩展 Args 参数以便模板可以推断返回类型?这只有在 auto 上才有可能吗?
【问题讨论】:
-
这可能不正确,但我建议你阅读good c++ book,尤其是一些高级的。像这样在撕裂的 sn-ps 中学习 C++ 非常困难。
标签: c++ templates generics variadic