我正在重构一些非常古老的遗留代码,这些代码充满了错误和非常可疑的做法,至少对于现代标准而言.现在我跑过一条线,我根本无法破译:
p并且k是类型int *
return p??!??!k?p?*p:sizeof(*k):0;
Run Code Online (Sandbox Code Playgroud)
当我看到它时,我无法相信自己的眼睛 - 我知道?运算符,但它的语法bool ? trueresult : falseresult和??操作符都没有意义(懒惰的评估确实不适用于此处),我无法在任何地方找到该神秘运算符的引用.
如果有人对此事有所了解,那真的很酷.
以前我用宏来测量函数调用的时间,每当我想快速检查时.现在,在C++ 11可用的情况下,我想最终删除预处理器代码的丑陋和平,并用以下内容替换它:
template <typename Functor, typename ... Args>
auto measure(Functor f, Args && ... args)
-> decltype(f(std::forward<Args>(args)...))
{
auto now = std::chrono::high_resolution_clock::now();
auto ret = f(std::forward<Args>(args)...);
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - now).count();
std::cout << "Time elapsed: " << elapsed << "ms" << std::endl;
return ret;
}
Run Code Online (Sandbox Code Playgroud)
对于返回某些东西(即没有void)的函数,它可以正常工作.所以我觉得我需要为void函数重载- 但是你不能仅仅在返回类型上重载函数.
我尝试使用一些模板魔法来解决这个问题,但无济于事; 编译器仍然抱怨函数measure定义了两次:
template <
typename Functor, typename ... Args,
typename ReturnType = typename std::enable_if<
!std::is_void<
typename std::result_of<Functor(Args...)>::type
>::value,
typename std::result_of<Functor(Args...)>::type
>::type
>
ReturnType measure(Functor f, …Run Code Online (Sandbox Code Playgroud)