[英]Variadic lambda vs variadic template function : stricly equivalent?
Consider this code snippet考虑这个代码片段
#include <chrono>
auto wrapAndCallLambda =
[] (auto fn, auto &&... params)
{
const auto start = std::chrono::system_clock::now();
auto result = fn(std::forward<decltype(params)>(params)...);
const auto end = std::chrono::system_clock::now();
const auto elapsed = end - start;
std::cout << "Elapsed time: " << elapsed.count() << "s";
return result;
};
template <typename Fn, typename... Args>
auto wrapAndCall(Fn fn, Args &&... params)
{
const auto start = std::chrono::system_clock::now();
auto result = fn(std::forward<decltype(params)>(params)...);
const auto end = std::chrono::system_clock::now();
const auto elapsed = end - start;
std::cout << "Elapsed time: " << elapsed.count() << "s";
return result;
}
Is it stricly equivalent?它是严格等价的吗? Is there any consideration to have in mind (not related to opinion and style) guiding which one to choose?
是否有任何考虑因素(与意见和风格无关)指导选择哪一个? Code bloating?
代码膨胀? Performances?
表演? Compile time if used extensively?
如果广泛使用编译时间?
wrapAndCallLambda
is an actual object, whereas wrapAndCall
is a template. wrapAndCallLambda
是一个实际的 object,而wrapAndCall
是一个模板。
This means that you can pass wrapAndCallLambda
directly to other functions, and you have to either explicitly pass &wrapAndCall<fn_type, arg1, arg2, ...>
, or wrap it into another lambda which forwards its arguments.这意味着您可以将
wrapAndCallLambda
直接传递给其他函数,并且您必须显式传递&wrapAndCall<fn_type, arg1, arg2, ...>
,或者将其包装到另一个转发其 arguments 的 lambda 中。
This also means that you cannot overload or specialize wrapAndCallLambda
, but you can easily write another overload for wrapAndCall
.这也意味着您不能重载或
wrapAndCallLambda
,但您可以轻松地为wrapAndCall
编写另一个重载。
You also can never find wrapAndCallLambda
via ADL.您也永远无法通过 ADL 找到
wrapAndCallLambda
。 Which may be a feature you want.这可能是您想要的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.