C++ c++;std::函数类型检查是否正确?
我希望使用std::function type from,以便在赋值时检查函数签名。但我不明白在这种情况下发生了什么C++ c++;std::函数类型检查是否正确?,c++,std-function,C++,Std Function,我希望使用std::function type from,以便在赋值时检查函数签名。但我不明白在这种情况下发生了什么 //g++ 5.4.0 #include <iostream> #include <functional> int f(int x) { std::cout << "//f:int->int\n"; return x; } int g(double x) { std::cout << "//g:doubl
//g++ 5.4.0
#include <iostream>
#include <functional>
int f(int x) { std::cout << "//f:int->int\n"; return x; }
int g(double x) { std::cout << "//g:double->int\n"; return 1; }
int main()
{
std::function<int(int)> fct;
fct = f; fct(1);
fct = g; fct(1);
}
//trace
//
//f:int->int
//g:double->int
//g++5.4.0
#包括
#包括
intf(intx){std::cout int
//g:double->int
f的行为是我想要的,但我认为“fct=g;”会导致编译时错误
请说明一下这个例子好吗?
std::function
是灵活的,下面使用了类型擦除
,因此如果您有签名为std::function
的object,它将接受任何可以用类型参数调用的并返回类型R
因此,对于std::function
,类型为intg(double);
的函数可以用类型int
参数调用,编译器只会将int
升级为double
如果您运行此代码
#include <iostream>
#include <functional>
int f(int x) { std::cout << x << " " << "//f:int->int\n"; return x; }
int g(double x) { std::cout << x << " " << "//g:double->int\n"; return 1; }
int main()
{
std::function<int(int)> fct;
fct = f; fct(1);
fct = g; fct(2.5);
}
#包括
#包括
int f(int x){std::coutstd::function
接受任何可调用的参数,其中参数可以转换,返回类型可以转换。如果不是,则会出现编译错误。例如:
int h(double& x);
std::function<int(int)> fct;
fct = h; // <- compiler error
inth(双倍&x);
std::功能fct;
fct=h;//要完成@gaurav的答案,请使用隐式转换完成Scenari(这确实让我感到惊讶,所以我为感兴趣的人添加了它)
//g++5.4.0
#包括
#包括
C类{
公众:
int i;
C(int _i):我(_i){std::cout谢谢@gaurav。如果我理解正确,也许f应该是int->double,g应该是double->double,fct应该是int->double。因此fct=g“强制”将第一个参数转换为int(测试它确实确认,但我不能100%确定,因为在你的示例中,g返回一个int…)@chetzacoalt,返回类型不应该区别上面的代码,因为它们每个都是int
,当代码调用fct(2.5)
时是的,因为fct::operator()
只接受int
,它将2.5转换为2。
//g++ 5.4.0
#include <iostream>
#include <functional>
class C{
public :
int i;
C(int _i) : i(_i) { std::cout << "//C::C(int " << i << ")\n"; }
};
class D{
public :
int i;
D(int _i) : i(_i) { std::cout << "//D::D(int " << i << ")\n"; }
D(C c) : i(c.i) { std::cout << "//implicit conversion D::D(C " << c.i << ")\n"; }
};
int f(C c) { std::cout << "//f:C->int : "; return c.i; }
int g(D d) { std::cout << "//g:D->int : "; return d.i; }
int main()
{
{
std::cout << "//--- test implicit conversion\n";
C c(1);
D d(2);
d=c;
}
{
std::function<int(C)> fct; C c(1); D d(2);
std::cout << "//direct calls\n";
std::cout << f(c) << "\n";
// std::cout << "//" << f(d) << "\n"; // no conversion D->C provided by class C -->> error: could not convert ‘d’ from ‘D’ to ‘C’
std::cout << g(d) << "\n";
std::cout << g(c) << "\n"; // implicit conversion, then g:D->int
}
{
std::cout << "//case function:C->int\n";
std::function<int(C)> fct; C c(1); D d(2);
fct = f; std::cout << "//" << fct(c) << "\n";
//std::cout << "//" << fct(d) << "\n"; // no conversion D->C provided by class C -->> error: could not convert ‘d’ from ‘D’ to ‘C’
fct = g; std::cout << "//" << fct(c) << "\n";
//std::cout << "//" << fct(d) << "\n"; // no conversion D->C provided by class C -->> no match for call to ‘(std::function<int(C)>) (D&)’
}
{
std::cout << "//appels via function : D -> int\n";
std::function<int(D)> fct; C c(1); D d(2);
//fct = f; // conversion D->C would be meaningless to f
// -->> error: no match for ‘operator=’ (operand types are ‘std::function<int(D)>’ and ‘int(C)’)
fct = g; std::cout << "//" << fct(d) << "\n";
std::cout << "//" << fct(c) << "\n"; // implicit conversion, then g:D->int
}
}
//trace
//
//--- test implicit conversion
//C::C(int 1)
//D::D(int 2)
//implicit conversion D::D(C 1)
//C::C(int 1)
//D::D(int 2)
//direct calls
//f:C->int : 1
//g:D->int : 2
//implicit conversion D::D(C 1)
//g:D->int : 1
//case function:C->int
//C::C(int 1)
//D::D(int 2)
//f:C->int : //1
//implicit conversion D::D(C 1)
//g:D->int : //1
//appels via function : D -> int
//C::C(int 1)
//D::D(int 2)
//g:D->int : //2
//implicit conversion D::D(C 1)
//g:D->int : //1