Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++ 为什么可以';编译器不能解决std::function参数的重载问题吗?_C++_Templates_C++11_Std_Overloading - Fatal编程技术网

C++ 为什么可以';编译器不能解决std::function参数的重载问题吗?

C++ 为什么可以';编译器不能解决std::function参数的重载问题吗?,c++,templates,c++11,std,overloading,C++,Templates,C++11,Std,Overloading,观察以下示例: #include <iostream> #include <functional> #include <cstdlib> void Print_Wrapper(std::function<void(int)> function); void Print(int param); int main(){ Print_Wrapper(Print); return EXIT_SUCCESS; } void Print_Wr

观察以下示例:

#include <iostream>
#include <functional>
#include <cstdlib>

void Print_Wrapper(std::function<void(int)> function);
void Print(int param);

int main(){

  Print_Wrapper(Print);

  return EXIT_SUCCESS;
}

void Print_Wrapper(std::function<void(int)> function){
  int i = 5;
  function(i);
}
void Print(int param){
  std::cout << param << std::endl;
}
#include <iostream>
#include <vector>
#include <cstdlib>

void Print_Wrapper(std::vector<int> param);
void Print(std::vector<int> param);
void Print(std::vector<float> param);

int main(){

  std::vector<int> vi{1,2,3};
  std::vector<float> vf{1.0,2.0,3.0};

  Print(vi); //prints int
  Print(vf); //prints float

  Print_Wrapper(vi); //prints int (no overload issue)
  Print_Wrapper(vf); //compiler error (as expected)

  return EXIT_SUCCESS;
}

void Print_Wrapper(std::vector<int> param){
  Print(param);
  return;
}
void Print(std::vector<int> param){
  std::cout << "int" << std::endl;
}
void Print(std::vector<float> param){
  std::cout << "float" << std::endl;
}
这会导致以下编译器错误:

main.cpp:11:22:错误:无法解析重载函数“Print” 基于转换为类型“std::function”
印刷包装(印刷)

能否解释一下为什么编译器无法解决重载问题?
Print\u Wrapper
只需要一个int函数——甚至不应该考虑float函数


另外,我应该怎么做才能解决这个问题

我记得这样的问题发生在
typename
或遗漏了什么东西的情况下,但这需要我制作
Print\u Wrapper
模板。
Print\u Wrapper
是否需要作为函数模板

我想我已经习惯了类似的功能,这很有效。例如:

#include <iostream>
#include <functional>
#include <cstdlib>

void Print_Wrapper(std::function<void(int)> function);
void Print(int param);

int main(){

  Print_Wrapper(Print);

  return EXIT_SUCCESS;
}

void Print_Wrapper(std::function<void(int)> function){
  int i = 5;
  function(i);
}
void Print(int param){
  std::cout << param << std::endl;
}
#include <iostream>
#include <vector>
#include <cstdlib>

void Print_Wrapper(std::vector<int> param);
void Print(std::vector<int> param);
void Print(std::vector<float> param);

int main(){

  std::vector<int> vi{1,2,3};
  std::vector<float> vf{1.0,2.0,3.0};

  Print(vi); //prints int
  Print(vf); //prints float

  Print_Wrapper(vi); //prints int (no overload issue)
  Print_Wrapper(vf); //compiler error (as expected)

  return EXIT_SUCCESS;
}

void Print_Wrapper(std::vector<int> param){
  Print(param);
  return;
}
void Print(std::vector<int> param){
  std::cout << "int" << std::endl;
}
void Print(std::vector<float> param){
  std::cout << "float" << std::endl;
}
#包括
#包括
#包括
无效打印包装(标准::矢量参数);
无效打印(标准::矢量参数);
无效打印(标准::矢量参数);
int main(){
std::向量vi{1,2,3};
std::向量vf{1.0,2.0,3.0};
打印(vi);//打印int
打印(vf);//打印浮点
Print_Wrapper(vi);//打印int(无过载问题)
Print_Wrapper(vf);//编译器错误(按预期)
返回退出成功;
}
无效打印包装(标准::向量参数){
打印(参数);
返回;
}
无效打印(标准::矢量参数){

std::cout您可以将这两个函数分配给
std::function
,因为这两个函数都可以用int调用


使用int版本强制选择正确的重载:
Print\u包装(静态\u转换(打印))在C++11中,
std::function
的构造函数可以接受任何可调用的对象,即使该对象格式不正确且无法调用该对象。在C++14中,重载解析是固定的,但这两个函数都可以用
int
调用,因此这两个函数都是有效的候选函数

您需要明确请求正确的函数:

Print_Wrapper(static_cast<void(*)(int)>(Print))
Print\u包装(静态\u转换(打印))

std::vector无法转换为std::vector,但int可以隐式转换为float
static\u cast(&Print)
@Sarang好的,如果我使用了完全不同的不能隐式转换的类型,我认为问题仍然存在。我的问题是否与int隐式转换为float有关,或者这会发生在任何两种类型上?你是说我的示例适用于C++14吗?这个解决方案有效。没有cast,它仍然不起作用k在C++14.Hmm中,最初我认为它应该在C++14中工作,但经过仔细研究,这个问题甚至在C++14中也存在,因为两者都可以用
int
调用。没有简单的方法可以说“这个转换是用户定义的对其他转换的首选转换”据我所知。可能使用了一个未使用的varargs包?看起来很脏。通过声明重载版本的
Print\u Wrapper
void Print\u Wrapper(void(*func)(int)){Print\u Wrapper(std::function(func));}