Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ “max”匹配的“std::function”没有重载_C++_Function - Fatal编程技术网

C++ “max”匹配的“std::function”没有重载

C++ “max”匹配的“std::function”没有重载,c++,function,C++,Function,此代码无法编译,并给出错误: test.cpp:12:4: error: no matching function for call to 'func' func(std::max<int>); ^~~~ test.cpp:4:6: note: candidate function not viable: no overload of 'max' matching 'std::function<const int &(const int &, con

此代码无法编译,并给出错误:

test.cpp:12:4: error: no matching function for call to 'func'
   func(std::max<int>);
   ^~~~
test.cpp:4:6: note: candidate function not viable: no overload of 'max' matching 'std::function<const int &(const int &, const int &)>' for 1st argument
void func(std::function<const int &(const int &, const int &)> a)
     ^
1 error generated.

我之前使用了一个函数指针,它的参数签名与std::function相同,并且可以正常工作。这是怎么回事

标准函数不是函数指针

模板函数的名称在被调用或转换为函数指针时会经历重载解析。当传递给std函数时,它不会经历重载解析

std::max可以引用多个函数;取一个初始值设定项列表或两个实参为int,并且每个实参都有带或不带比较器的重载

使用重载解析,除了其中一个之外,所有这些都将被丢弃。如果没有,结果是不明确的

int x00 = std::max<int>( std::initializer_list<int>{1,2,3,4} )
int x01 = std::max<int>( 2, 4 );
int x10 = std::max<int>( std::initializer_list<int>{1,2,3,4}, [](int a, int b){ return b>a; } )
int x11 = std::max<int>( 2, 4, [](int a, int b){ return b>a; } );
在这里,我们正在做过载解析。我们选择需要两个整数的std::max

我有时把它写成宏

#define RETURNS(...) \
  noexcept(noexcept( __VA_ARGS__ )) \
  -> decltype( __VA_ARGS__ ) \
  { return __VA_ARGS__; }
#define CALL(...) \
  [](auto&&...args) \
  RETURNS( __VA_ARGS__( decltype(args)(args)... ) )
给我们:

std::function<int const&(int const&, int const&)> f = CALL(std::max<int>);

在这里,我延迟重载解析,直到调用传递f的可调用对象之后。在这一点上,类型是已知的,所以一切都正常。

标准函数不是函数指针

模板函数的名称在被调用或转换为函数指针时会经历重载解析。当传递给std函数时,它不会经历重载解析

std::max可以引用多个函数;取一个初始值设定项列表或两个实参为int,并且每个实参都有带或不带比较器的重载

使用重载解析,除了其中一个之外,所有这些都将被丢弃。如果没有,结果是不明确的

int x00 = std::max<int>( std::initializer_list<int>{1,2,3,4} )
int x01 = std::max<int>( 2, 4 );
int x10 = std::max<int>( std::initializer_list<int>{1,2,3,4}, [](int a, int b){ return b>a; } )
int x11 = std::max<int>( 2, 4, [](int a, int b){ return b>a; } );
在这里,我们正在做过载解析。我们选择需要两个整数的std::max

我有时把它写成宏

#define RETURNS(...) \
  noexcept(noexcept( __VA_ARGS__ )) \
  -> decltype( __VA_ARGS__ ) \
  { return __VA_ARGS__; }
#define CALL(...) \
  [](auto&&...args) \
  RETURNS( __VA_ARGS__( decltype(args)(args)... ) )
给我们:

std::function<int const&(int const&, int const&)> f = CALL(std::max<int>);

在这里,我延迟重载解析,直到调用传递f的可调用对象之后。在这一点上,类型是已知的,所以一切都正常。

Typo?max需要包含@RichardCritten这不是这里的问题,因为代码使用函数指针方法,但我只是添加了该库以进行验证,它没有修复错误。我认为functional includes algorithmOne std header可以包含另一个,但从来没有任何保证。我添加了include@OP FWIW,而不是争论,只是添加它。它消除了一个潜在的问题点,你想要尽可能少的问题点,以便对你的问题做出好的反应。打字错误?max需要包含@RichardCritten这不是这里的问题,因为代码使用函数指针方法,但我只是添加了该库以进行验证,它没有修复错误。我认为functional includes algorithmOne std header可以包含另一个,但从来没有任何保证。我添加了include@OP FWIW,而不是争论,只是添加它。它消除了一个潜在的问题点,您希望尽可能少的问题点,以便对您的问题做出良好的反应。您使用decltypeargsargs做什么。。。?你的意思是std::forwardargs…?@NathanOliver不,我的意思是输入更少的字符,然后做完全相同的事情。我正在将每个参数转换为它声明为的类型。我不是有条件地移动。在这种情况下,结果是相同的,因为args正在转发引用,但较短,在其他上下文中则不同。以前没有使用decltype,但是如果要强制转换某些内容,通常不需要在要强制转换的类型周围加一对括号吗?那么在这种情况下,它应该是std::maxDecTypeArgsArgs…?您使用DecTypeArgsArgs做什么。。。?你的意思是std::forwardargs…?@NathanOliver不,我的意思是输入更少的字符,然后做完全相同的事情。我正在将每个参数转换为它声明为的类型。我不是有条件地移动。在这种情况下,结果是相同的,因为args正在转发引用,但较短,在其他上下文中则不同。以前没有使用decltype,但是如果要强制转换某些内容,通常不需要在要强制转换的类型周围加一对括号吗?那么在这种情况下,它应该是std::maxdecltypeargsargs。。。?
std::function<int const&(int const&, int const&)> f = CALL(std::max<int>);