C++ 3.奇怪的行为。。“int(int)、int(std::string)”与“int(int)、int(std::string)、std::string(std::string)”有何不同?
这就是所谓的伟大自由。它可以接受任何类型的函数,并自动决定调用哪个函数。它类似于boost函数,但更好。 以下是2个代码示例(浏览器可以查看boost svn)。这是我的代码,它不是编译的,而是基于它们的:C++ 3.奇怪的行为。。“int(int)、int(std::string)”与“int(int)、int(std::string)、std::string(std::string)”有何不同?,c++,boost,overloading,boost-function,C++,Boost,Overloading,Boost Function,这就是所谓的伟大自由。它可以接受任何类型的函数,并自动决定调用哪个函数。它类似于boost函数,但更好。 以下是2个代码示例(浏览器可以查看boost svn)。这是我的代码,它不是编译的,而是基于它们的: #include <string> #include <boost/detail/lightweight_test.hpp> #include <boost/overload.hpp> using boost::overload; templat
#include <string>
#include <boost/detail/lightweight_test.hpp>
#include <boost/overload.hpp>
using boost::overload;
template<class out, class in>
out foo(in O )
{
std::cout << "yes we can!";
return out();
}
int main()
{
//// works
//overload<int (int ), int (std::string )> f;
//// works
//int (*foo1) (int ) = &foo<int, int>;
//int (*foo2) (std::string ) = &foo<int, std::string>;
//f.set(foo1);
//f.set(foo2);
// or we can use
//// does also work
//f.set<int (int )>(&foo<int, int>);
//f.set<int (std::string )>(&foo<int, std::string>);
////
overload<int (int ), int (std::string ), std::string (std::string) > f;
//// but when we do this
//f.set<int (int )>(&foo<int, int>);
//f.set<int (std::string )>(&foo<int, std::string>);
//f.set<int (std::string )>(&foo<std::string, std::string>);
//// or this:
int (*foo1) (int ) = &foo<int, int>;
int (*foo2) (std::string ) = &foo<int, std::string>;
std::string (*foo3) (std::string ) = &foo<std::string, std::string>;
f.set(foo1);
f.set(foo2);
f.set(foo3);
//// we get compile error
BOOST_ASSERT( f(0) == 1 );
BOOST_ASSERT( f("hi") == 2 ); // here we get Error 1 error C3066: there are multiple ways that an object of this type can be called with these arguments
return boost::report_errors();
}
#包括
#包括
#包括
使用boost::重载;
模板
out foo(in O)
{
STD::CUT过载解决方案只考虑参数类型;它不考虑返回类型。因此,在过载解决过程中,<代码> int(STD::String)< /C> >与<代码> STD::String(STD::String)< /C> >不符。
既然这个库必须依赖C++语言的超载能力,它也无法区分这两个函数。 <代码> @ /Cord> MyWaljson,我确信你现在已经注意到了,用“<代码>”这样的短语开始提问,所以这个格子LIB XXXX < /COD>不太酷。你能试着在格式化/预处理中付出更多的努力吗?问你的问题吗?你似乎问了很多问题,但不想花太多的精力让问题变得令人满意。谢谢你的考虑!