完美转发void和非void返回函数 [英] Perfect forwarding for void and non-void returning functions
问题描述
以前,当我想快速检查时,我使用一个宏来测量函数调用的时间。现在,使用C ++ 11可用,我想最终删除预处理器代码的丑陋的和平,并替换为这样:
Previously I was using a macro to measure the time a function call took whenever I wanted to quickly check that. Now, with C++11 available, I would like to finally remove that ugly peace of preprocessor code and replace it with something like this:
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;
}
对于返回值的函数> void )。所以我觉得我需要一个重载的 void
函数 - 但你不能重载函数只是在返回类型。
Which works fine for functions that return something (i.e. not void
). So I felt like I needed an overload for void
functions - but you cannot overload a function just on return type.
我试着使用一些模板魔法走动这个问题,但没有效果;编译器仍然抱怨函数 measure
定义了两次:
I tried to walk around this problem using some template magic, but to no avail; the compiler still complains that the function measure
is defined two times:
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, 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;
}
template <
typename Functor, typename ... Args,
typename ReturnType = typename std::enable_if<
std::is_void<
typename std::result_of<Functor(Args...)>::type
>::value
>::type
>
ReturnType measure(Functor f, Args && ... args)
{
auto now = std::chrono::high_resolution_clock::now();
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;
}
有办法解决这个问题吗?
Is there a way around this?
UPDATE
这里是我现在使用的功能R. Martinho Fernandes:
Here is the function I am now using thanks to R. Martinho Fernandes:
template <typename Functor, typename ... Args>
auto measure(Functor f, Args && ... args)
-> decltype(f(std::forward<Args>(args)...))
{
struct scoped_timer
{
scoped_timer() : now_(std::chrono::high_resolution_clock::now()) {}
~scoped_timer()
{
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;
}
private:
std::chrono::high_resolution_clock::time_point const now_;
} scoped_timer;
return f(std::forward<Args>(args)...);
}
推荐答案
问题是,参数不适用于不同的模板,默认函数参数不对不同的重载做同样的方式。有一些方法可以解决这个问题,我在我的 Remastered enable_if 文章。
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.
但是,我不会这样做。我将简单地利用这样的事实,在通用代码中,您可以 return
void,并使用RAII打印耗用的时间:
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)...);
}
scoped_timer
可以简单地写:在构造函数中保存现在
,并在析构函数中计算并输出 elapsed
。
The scoped_timer
class can be written trivially: save now
in the constructor, and compute and output elapsed
in the destructor.
这篇关于完美转发void和非void返回函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!