gpt4 book ai didi

c++ - 为什么 std::function::argument_type 已被弃用?

转载 作者:IT老高 更新时间:2023-10-28 22:03:00 26 4
gpt4 key购买 nike

我在 cppreference 上见过std::function::argument_type 在 C++17 中已被弃用。背后的原因是什么?什么 ISO WG21 论文提出了这个建议?

最佳答案

相关论文是P0005R4 (这是被投票纳入标准草案的论文)和 P0090R0 (由 P0005R4 引用)。

来自 P0090R0 的报价:

Q2. What's wrong with result_type, etc.?

A2. These C++98/03/TR1-era typedefs predated decltype and perfect forwarding. Previously, generic code had to request information from function objects before adapting them. Now, manually communicating that information is unnecessary. decltype supersedes result_type, because the compiler can simply report what the result of calling a function object with specific arguments will be. And perfect forwarding supersedes the argument_type family, since adaptors can simply take/store/forward arbitrary arguments.

In fact, these typedefs are worse than useless. They're counterproductive, because many callable objects lack them. Function pointers and pointers to members have always lacked them. ptr_fun(), which wrapped function pointers with these typedefs, was recently removed (see [1] again). Most importantly, lambdas have always lacked these typedefs, and they are the most important function objects of all. Generic lambdas are even more incompatible.

What this means is that if a user attempts to write generic code by using the result_type family of typedefs, their code won't be generic - it'll fail to handle lambdas, generic lambdas, function pointers, etc.

These typedefs should be removed because they've become actively harmful to modern code.

关于c++ - 为什么 std::function::argument_type 已被弃用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35907454/

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