Boost Overload 奇怪的行为.. `int (int ), int (std::string )` 与 `int (int ), int (std::string ), std::string (std::string)` 有何不同?
所以有一个名为 OverLoad 的伟大库(链接到可下载的 svn 目录,lib 仅是标头)。它可以接受任何类型的函数,并自动决定您要调用哪一个。它类似于 boost 函数,但更好。 这里有2个代码示例(浏览器可以查看boost svn) 一个 两个。这是我的代码,无法编译并且基于它们:
#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();
}
所以我想知道如何解决这个问题?
So there is that great lib called OverLoad (link to downloadable svn directory, lib is header only). it can accept functions of any types into it and automatically decide which one you are calling. It is like boost function but better.
Here are 2 code samples (browser can view boost svn) one two. and here is my code that does not compile and is based on them:
#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();
}
So I wonder how to get around this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重载解析仅考虑参数类型;它不考虑返回类型。因此,在重载解析期间,int (std::string) 与 std::string(std::string) 无法区分。
由于该库必须依赖于C++语言的重载功能,因此它也无法区分这两个函数。
Overload resolution only considers the parameter types; it does not consider the return type. So, during overload resolution,
int (std::string)
is indistinguishable fromstd::string(std::string)
.Since this library has to rely on the C++ language's overloading capabilities, it too cannot distinguish between the two functions.