使用"std::enable_if"转发包装失败

Forwarding wrapper using `std::enable_if` failing

本文关键字:quot 转发 包装 失败 if std 使用 enable      更新时间:2023-10-16

我正在尝试创建一个转发包装器函数,该函数对c++14中的函数的调用进行计时。我需要处理2类型,

  • 一种是对不返回值的函数进行计时,而

  • 其他未返回

在这里跳过的调用前后可能需要执行一些操作,所以我不能只返回func(),它同时处理voidnon-void类型。

这是void函数的包装器。

template<typename T, typename ...U>
auto time_function(T&& func, U&& ...args) -> typename enable_if<is_same<decltype(func(args...)), void>::value>::type
{
std::cout << "timing void function" << std::endl;
std::forward<T>(func)(std::forward<U>(args)...);
std::cout << "timing over" << std::endl;
}

non-void函数的包装器

template<typename T, typename ...U>
auto time_function(T&& func, U&& ...args) -> typename enable_if < !is_same<decltype(func(args...)), void), decltype(func(args...)) > ::value > ::type
{
std::cout << "timing returning function" << std::endl;
auto val = std::forward<T>(func)(std::forward<U>(args)...);
std::cout << "timing over" << std::endl;
return val;
}
int main()
{
time_function(foo, 2);
int i = time_function(&foo_return, 1); //this generates an error
//std::cout<<i<<std::endl;
}

foo是返回void的函数,foo_return返回整数。生成的错误是

<source > :28 : 129 : error : template argument 1 is invalid
auto time_function(T && func, U && ...args) -> typename enable_if<!is_same<decltype(func(args...)), void>, decltype(func(args...))>::value > ::type
^
<source>:28 : 55 : error : expected nested - name - specifier before 'enable_if'
auto time_function(T && func, U && ...args) -> typename enable_if<!is_same<decltype(func(args...)), void>, decltype(func(args...))>::value > ::type
^ ~~~~~~~~
<source>:28 : 129 : error : template argument 1 is invalid
auto time_function(T && func, U && ...args) -> typename enable_if<!is_same<decltype(func(args...)), void>, decltype(func(args...))>::value > ::type
^
<source>:28 : 129 : error : template argument 1 is invalid
<source> : 28 : 129 : error : template argument 1 is invalid
<source> : 28 : 55 : error : expected initializer before 'enable_if'
auto time_function(T && func, U && ...args) -> typename enable_if<!is_same<decltype(func(args...)), void>, decltype(func(args...))>::value > ::type
^ ~~~~~~~~
<source>: In function 'int main()' :
<source> : 42 : 41 : error : no matching function for call to 'time_function(int (*)(int), int)'
int i = time_function(&foo_return, 1); //error -
^
<source>:20 : 6 : note : candidate : template<class T, class ... U> typename std::enable_if<std::is_same<decltype (func(time_function::args ...)), void>::value>::type time_function(T&&, U && ...)
auto time_function(T && func, U && ...args) -> typename enable_if<is_same<decltype(func(args...)), void>::value>::type
^ ~~~~~~~~~~~~
<source> : 20 : 6 : note : template argument deduction / substitution failed :
<source> : In substitution of 'template<class T, class ... U> typename std::enable_if<std::is_same<decltype (func(time_function::args ...)), void>::value>::type time_function(T&&, U&& ...) [with T = int (*)(int); U = {int}]' :
<source> : 42 : 41 : required from here
<source> : 20 : 6 : error : no type named 'type' in 'struct std::enable_if<false, void>'

据我所知,包装是正确的,哪里错了?我正在使用is_same检查函数的返回类型是否为void,如果是,则使用enable_if声明我希望的返回类型。

@rmawatson已经指出了语法错误。然而,我想提及一些改进。

由于您使用的是c++14,因此您可以通过使用助手类型std::enable_if_t,使用不太详细的std::enable_if版本来实现同样的目的

其次,从检查类型是否为void的标准来看,std::is_void是更好(或合适(的特征,这也将节省一些类型。使用变量模板(也是从c++14开始(,时间会短得多(请参阅此处的实时演示(

#include <iostream>
#include <type_traits> // std::enable_if_t, std::is_void
// variable templates
template<class T> constexpr bool is_void_v = std::is_void<T>::value;
template<typename T, typename ...U>
auto time_function(T&& func, U&& ...args) -> std::enable_if_t<::is_void_v<decltype(func(args...))>>
{
// ... code here
}
template<typename T, typename ...U>
auto time_function(T&& func, U&& ...args)
-> std::enable_if_t<!::is_void_v<decltype(func(args...))>, decltype(func(args...))>
{
// ... code here
}

但是,如果您可以访问c++17,您可以使用if constexpr在一个函数中简单地编写两个逻辑。只为未来@todo列表 .(请参阅此处直播(

#include <type_traits> // std::enable_if_t, std::is_void_v
template<typename T, typename ...U>
auto time_function(T&& func, U&& ...args)
{
if constexpr (std::is_void_v<decltype(func(args...))>)
{
// ... timing void function"
} 
else
{
// ... timing returning function
}
}

您有一个语法错误,

-> typename enable_if<!is_same<decltype(func(args...)), void),decltype(func(args...))>::value>::type

-> typename enable_if<!is_same<decltype(func(args...)), void>::value, decltype(func(args...))>::type

#include <type_traits>
#include <iostream>
using namespace std;
template<typename T, typename ...U>
auto time_function(T&& func, U&& ...args) 
-> typename enable_if<is_same<decltype(func(args...)), void>::value>::type
{
std::cout<<"timing void function"<<std::endl;
std::forward<T>(func)(std::forward<U>(args)...);
std::cout<<"timing over"<<std::endl;
}
template<typename T, typename ...U>
auto time_function(T&& func, U&& ...args)
-> typename enable_if<!is_same<decltype(func(args...)), void>::value, decltype(func(args...))>::type
{
std::cout<<"timing returning function"<<std::endl;
auto val = std::forward<T>(func)(std::forward<U>(args)...);
std::cout<<"timing over"<<std::endl;
return val;
}

void foo(int){}
int foo_return(int){return 0;}
int main()
{
time_function(foo, 2);
int i = time_function(&foo_return, 1); //this generates an error
std::cout<<i<<std::endl;
}

演示