如何使用可变参数使模板推导 return 类型的函数
How to make template deduce return type of function with variadic arguments
堆栈溢出的某人写了一个有趣的方法来将 lambda 或仿函数捕获到您自己的 class。我试图简化它,我想我已经接近了,但遇到了一些麻烦。他们的例子是:
// 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 class 中包含的指针不需要,因为您可以调用函数对象本身,而不是调用指针给运营商()。所以我尝试了这个:
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 参数以便模板可以推导出 return 类型?这只能使用自动吗?
decltype(e)
接受一个 expression e
并求值为 expression 的类型。您需要提供一个 expression 来表示对存储的 lambda 的调用:
auto operator()(Args... args)
-> decltype(std::declval<OT>()(std::declval<Args>()...))
在这种情况下,我使用 std::declval
创建一个 "fake instance" 对象,可用于推导目的,而不实际调用任何构造函数。
让我们进一步分解:
-> decltype(
std::declval<OT>() // create a fake instance of `OT`
( // invoke it
std::declval<Args>()... // create a fake instance of each argument
// type, expanding `Args...`
)
)
顺便说一下,您仍然应该 std::forward
调用 funcObj
中的参数,因为可能有一些 rvalue 引用 需要传播再往下:
auto operator()(Args... args)
{
return funcObj(std::forward<Args>(args)...);
}
堆栈溢出的某人写了一个有趣的方法来将 lambda 或仿函数捕获到您自己的 class。我试图简化它,我想我已经接近了,但遇到了一些麻烦。他们的例子是:
// 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 class 中包含的指针不需要,因为您可以调用函数对象本身,而不是调用指针给运营商()。所以我尝试了这个:
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 参数以便模板可以推导出 return 类型?这只能使用自动吗?
decltype(e)
接受一个 expression e
并求值为 expression 的类型。您需要提供一个 expression 来表示对存储的 lambda 的调用:
auto operator()(Args... args)
-> decltype(std::declval<OT>()(std::declval<Args>()...))
在这种情况下,我使用 std::declval
创建一个 "fake instance" 对象,可用于推导目的,而不实际调用任何构造函数。
让我们进一步分解:
-> decltype(
std::declval<OT>() // create a fake instance of `OT`
( // invoke it
std::declval<Args>()... // create a fake instance of each argument
// type, expanding `Args...`
)
)
顺便说一下,您仍然应该 std::forward
调用 funcObj
中的参数,因为可能有一些 rvalue 引用 需要传播再往下:
auto operator()(Args... args)
{
return funcObj(std::forward<Args>(args)...);
}