英文:
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<decltype(&abc::now)>::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
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<abc>().now() );
or simply
using def = std::chrono::time_point<std::chrono::system_clock>;
alternatively
using 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论