我一直在尝试找出 lambda 表达式的返回类型,以了解该函数是否返回 void
。
我的第一个想法是使用 std::invoke_result
,但是它没有按预期工作,因为它不能与 std::is_same
一起使用,后者需要两种类型。
这是我一直在尝试的
auto lamb = [] ( int x ) { cout << x << endl; };
is_same<(invoke_result<decltype(lamb),int>),void>;
错误是:
trabapply2.cc:32:50: error: template argument 1 is invalid
is_same<(invoke_result<decltype(lamb),int>),void>;
有什么建议吗?
最佳答案
您应该删除 invoke_result
周围多余的括号,并从 invoke_result
获取 type
,并从 invoke_result
获取 value
is_same
。例如
is_same<invoke_result<decltype(lamb), int>::type, void>::value // gives true or false
或
is_same_v<invoke_result_t<decltype(lamb), int>, void> // since C++17
关于c++ - 如何比较 C++ 中 lambda 函数的返回类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58126490/