Perfect forwarding for void and non-void returning functions

我的未来我决定 提交于 2019-11-28 20:52:11

The problem is that default template arguments don't make for different templates, the same way that default function arguments don't make for different overloads. There are some ways around this, and I described them in my Remastered enable_if article.

However, I would not do that. I would simply take advantage of the fact that in generic code you can "return void", and use RAII to print out the elapsed time:

template <typename Functor, typename ... Args>
auto measure(Functor f, Args && ... args)
    -> decltype(f(std::forward<Args>(args)...))
{
    scoped_timer timer;
    return f(std::forward<Args>(args)...);
}

The scoped_timer class can be written trivially: save now in the constructor, and compute and output elapsed in the destructor.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!