【发布时间】:2021-01-25 18:12:01
【问题描述】:
我正在尝试创建一个模板类,它需要一个 lambda 作为输入,将其存储,并将 lambda 的 type = return type 的一些元素存储在 vector 中。但我不知道如何获得该类型,即使在构建实例时,lambda 也知道它的返回类型。
我希望在不提供模板参数的情况下构建类(如std::array a{1, 2, 3})。我尝试使用decltype(F::operator(double x)),但它不起作用。
#include <vector>
template<typename F>
struct Foo {
using value_t = decltype(F::operator(double x))// <--- here I need to get the return type of the
// call operator of F!
Foo(const F& f) : _f(f), _vals(10, value_t{}), _has_vals(10, false) {}
value_t operator()(int i) {
if (_has_vals[i])
return _vals[i];
else {
_has_vals[i] = true;
_vals[i] = _f(i);
}
}
F _f;
std::vector<value_t> _vals;
std::vector<bool> _has_vals;
};
#include <iostream>
int main() {
Foo foo([](double x){ return 42; }); // <--- here I know that the lambda returns an int!
std::cout << foo(3) << "\n";
return 0;
};
【问题讨论】:
-
可能是
decltype(std::declval<F>()(1.0))? -
无关:在
value_t Foo<F>::operator()(int i)中,您需要检查i<0 || i>9是否存在,否则可能会出现问题。您还需要在两个分支中返回value_t。您目前有 UB。 -
如果 lambda 的参数类型不是默认可构造的,我想没有办法吧?因为无法在默认实例上调用调用运算符。
-
std::declval<NonDefaultConstructibleType>()? -
@lucmobz -
std::declval()正好可以解决这类问题;按照 Ted Lyngmo 的建议,您可以尝试使用decltype(std::declval<F>()(std::declval<NonDefaultConstructibleType>()))
标签: c++ templates c++20 return-type decltype