C++23 中是否有支持 const/ref/noexcept 的可复制函数包装器?

Ben*_*uch 6 c++ c++23

C++23 引入了对隐式参数和函数本身的std::move_only_function支持。constl/r-value-referencethisnoexcept(true/false)

\n

旧的std::function缺乏这些超载。

\n
#include <functional>\n\nint main(){\n    std::function<void()>([]noexcept(true){});          // okay \xe2\x9c\x93\n    std::function<void()>([]noexcept(false){});         // okay \xe2\x9c\x93\n    std::function<void()noexcept>([]noexcept(true){});  // not supported\n    std::function<void()noexcept>([]noexcept(false){}); // not supported\n\n    std::move_only_function<void()>([]noexcept(true){});          // okay \xe2\x9c\x93\n    std::move_only_function<void()>([]noexcept(false){});         // okay \xe2\x9c\x93\n    std::move_only_function<void()noexcept>([]noexcept(true){});  // okay \xe2\x9c\x93\n    std::move_only_function<void()noexcept>([]noexcept(false){}); // fails \xe2\x9c\x93\n\n    std::function<void()>([i=0]{});             // okay \xe2\x9c\x93\n    std::function<void()>([i=0]mutable{});      // okay \xe2\x9c\x93\n    std::function<void()const>([i=0]{});        // not supported\n    std::function<void()const>([i=0]mutable{}); // not supported\n\n    std::move_only_function<void()>([i=0]{});             // okay \xe2\x9c\x93\n    std::move_only_function<void()>([i=0]mutable{});      // okay \xe2\x9c\x93\n    std::move_only_function<void()const>([i=0]{});        // okay \xe2\x9c\x93\n    std::move_only_function<void()const>([i=0]mutable{}); // fails \xe2\x9c\x93\n}\n
Run Code Online (Sandbox Code Playgroud)\n

不幸的是,顾名思义,std::move_only_function无法复制。

\n
#include <functional>\n#include <vector>\n\nint main(){\n    auto const fn = []{};\n\n    std::function<void()> a = fn;\n    std::vector<std::function<void()>> a_list{a, a};\n\n    std::move_only_function<void()> b = fn;\n    std::vector<std::move_only_function<void()>> b_list{b, b}; // error\n}\n
Run Code Online (Sandbox Code Playgroud)\n

是否有支持复制的模拟函数包装器?如果 C++23 中不是这种情况,是否有提供此功能的库?

\n

我需要一个作为函数所有者的数据类型。

\n

Bar*_*rry 6

是否有支持复制的模拟函数包装器?如果 C++23 中不是这种情况,是否有提供此功能的库?

不。

在 C++26 中会有std::copyable_function( P2548 )。有一个参考实现