- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在对我关于 std::conjunction
的问题的评论中没有短路(Conjuction template doesn't short circuit),我被推荐std::is_invocable_r
作为解决我的问题的方法。然而,当我尝试使用它时,我发现了一些非常奇怪的行为。例如这段代码失败了两个断言:
#include <type_traits>
int main()
{
static_assert(!std::is_invocable_r_v<void, int(int), int>);
static_assert(std::is_convertible_v<int,void>);
return 0;
}
注意 is_invocable_r
的描述在 cppreference :
Determines whether
Fn
can be invoked with the argumentsArgTypes...
to yield a result that is convertible toR
.
很明显int
不可转换为 void
,第二个断言证实了这一点。问题是为什么 std::is_invocable_r_v<void, int(int), int>
产生一个 true
那么值。这是一个活生生的例子:https://godbolt.org/z/HywH7D
请注意,可以使用 std::is_void_v<std::invoke_result_t<int(int),int>>
在这里得到正确的答案(https://godbolt.org/z/YMvc47),但这不是我的问题。 (而且它没有解决我的连词问题)。
最佳答案
紧接着
Determines whether
Fn
can be invoked with the argumentsArgTypes...
to yield a result that is convertible toR
. ...
它给出了更严格的定义:
... Formally, determines whether
INVOKE<R>(declval<Fn>(), declval<ArgTypes>()...)
is well formed when treated as an unevaluated operand, whereINVOKE
is the operation defined in Callable
在 Callable 据解释 INVOKE<void>
是一个特例。而不是将返回值隐式转换为 R
, 它使用 static_cast<void>(...)
(对于任何返回类型都是格式正确的)。
关于c++ - is_invocable_r 忽略返回参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57019152/
在对我关于 std::conjunction 的问题的评论中没有短路(Conjuction template doesn't short circuit),我被推荐std::is_invocable_
我是一名优秀的程序员,十分优秀!