gpt4 book ai didi

c++ - is_invocable_r 忽略返回参数

转载 作者:太空狗 更新时间:2023-10-29 21:31:51 26 4
gpt4 key购买 nike

在对我关于 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 arguments ArgTypes... to yield a result that is convertible to R.

很明显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 arguments ArgTypes... to yield a result that is convertible to R. ...

它给出了更严格的定义:

... Formally, determines whether INVOKE<R>(declval<Fn>(), declval<ArgTypes>()...) is well formed when treated as an unevaluated operand, where INVOKE is the operation defined in Callable

Callable 据解释 INVOKE<void>是一个特例。而不是将返回值隐式转换为 R , 它使用 static_cast<void>(...) (对于任何返回类型都是格式正确的)。

关于c++ - is_invocable_r 忽略返回参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57019152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com