migrate std::invoke_result back to result_of

huangapple go评论84阅读模式
英文:

migrate std::invoke_result back to result_of

问题

我正在尝试使用下面的代码片段,该代码在一个无法使用C++17的旧平台上使用std::invoke_result,最新支持的是C++11:

using abc = std::chrono::system_clock;
using def = typename std::invoke_result<decltype(&abc::now)>::type;

有没有关于如何使等效的代码在C++11下编译的想法?我已经尝试过查看result_of,但尚未成功。

谢谢,

英文:

I'm trying to use a bit of code, below, that uses std::invoke_result on an old platform where it's not possible to use c++17, latest is c++11

using abc = std::chrono::system_clock;
using def = typename std::invoke_result&lt;decltype(&amp;abc::now)&gt;::type;

any ideas on how to have equivalent code to compile with c++11? I've tried looking into result_of, without success yet.

thank you,

答案1

得分: 3

You can find a possible implementation here: https://en.cppreference.com/w/cpp/types/result_of. If I am not mistaken, it includes a C++11 conformant implementation.

However, outside of generic code, most uses are much simpler to replace. In your case it is

使用 abc = std::chrono::system_clock;
使用 def = decltype( std::declval().now() );

or simply

使用 def = std::chrono::time_pointstd::chrono::system_clock;

alternatively

使用 def = std::chrono::time_point< abc >;

I actually do not understand why invoke_result was used in the first place in your code. It is helpful in generic code or in general when you need to disambiguate different overloads with different return types. Neither is the case here, though that might be just due to a too simplistic example.

英文:

You can find a possible implementation here: https://en.cppreference.com/w/cpp/types/result_of. If I am not mistaken, it includes a C++11 conformant implementation.

However, outside of generic code, most uses are much simpler to replace. In your case it is

using abc = std::chrono::system_clock;
using def = decltype( std::declval&lt;abc&gt;().now() );

or simply

using def = std::chrono::time_point&lt;std::chrono::system_clock&gt;;

alternatively

using def = std::chrono::time_point&lt; abc &gt;;

I actually do not understand why invoke_result was used in the first place in your code. It is helpful in generic code or in general when you need to disambiguate different overloads with different return types. Neither is the case here, though that might be just due to a too simplistic example.

huangapple
  • 本文由 发表于 2023年6月29日 15:55:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定