gpt4 book ai didi

c++ - 在标准下调用 std::function 是否非法?

转载 作者:IT老高 更新时间:2023-10-28 12:51:53 24 4
gpt4 key购买 nike

所有报价均来自N3797 .

4/3 [转化]

An expression e can be implicitly converted to a type T if and only if the declaration T t=e; is well-formed, for some invented temporary variable t

这意味着没有表达式可以隐式转换为 void ,如 void t=e对所有表达式都是非法的 e .如果 e 则更是如此。是 void 类型的表达式,如 void(3) .

所以是 void 类型的表达式不能隐式转换为 void .

这导致我们:

20.9.2/2 要求 [func.require]

Define INVOKE (f, t1, t2, ..., tN, R) as INVOKE (f, t1, t2, ..., tN) implicitly converted to R .

简而言之,INVOKE(f, t1, t2, ..., tN, R)R 时永远无效是 void ,因为没有任何东西(包括 void )可以隐式转换为 void .

因此,所有 std::function<void(Args...)>有特性!*this因此不能被调用,因为唯一没有 !*this 的构造函数作为后置条件(或不从同类型的另一个 function 复制此类状态)需要 Callable参数之一。

20.9.11.2/7 类模板功能[func.wrap.func]

Requires: F shall be CopyConstructible . f shall be Callable ( 20.9.11.2 ) for argument types ArgTypes and return type R . The copy constructor and destructor of A shall not throw exceptions.

20.9.11.2/2 类模板功能[func.wrap.func]

A callable object f of type F is Callable for argument types ArgTypes and return type R if the expres- sion INVOKE (f, declval()..., R) , considered as an unevaluated operand (Clause 5 ), is well formed ( 20.9.2 ).

如上所示,没有Callable std::function<void(Args...)> 的表达式.

如果有这样的 std::function<void(Args...)>在哪里找到,调用 operator()将是不正确的:

调用 [func.wrap.func.inv]

Effects: INVOKE (f, std::forward(args)..., R) ( 20.9.2 ), where f is the target ob- ject ( 20.9.1 ) of *this .

作为 INVOKE(f, std::forward<ArgTypes>(args)..., void)所有论点和 f 格式错误.

这个推理合理吗?

最佳答案

是的,您的分析是正确的;我得出了同样的结论here .

根据 Daniel Kruegler 的说法,本期 should appear on the library defect list subsequent to the next mailing :

A corresponding library issue has already been submitted, but is not yet visible in the issue list.

希望一旦它变得可见,我们也会对是否允许构建 std::function 有一个决定性的答案。签名返回void传递带有签名返回非 void (Using `std::function<void(...)>` to call non-void function) 的可调用对象。


更新:输入为 LWG 2420 ,已解决,有利于特殊情况 void返回类型为static_cast调用函数的结果为void .这意味着可调用的返回非 void可以是 std::function<void(...)> 的目标. LWG2420 作为对 C++14 的发布后更正应用;同时,我所知道的所有编译器都有效地将这种行为作为 C++11 模式的扩展。

关于c++ - 在标准下调用 std::function<void(Args...)> 是否非法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25395605/

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