Boost Overload 奇怪的行为.. `int (int ), int (std::string )` 与 `int (int ), int (std::string ), std::string (std::string)` 有何不同?

发布于 2024-12-19 05:47:12 字数 2092 浏览 3 评论 0原文

所以有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妞丶爷亲个 2024-12-26 05:47:12

重载解析仅考虑参数类型;它不考虑返回类型。因此,在重载解析期间,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 from std::string(std::string).

Since this library has to rely on the C++ language's overloading capabilities, it too cannot distinguish between the two functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文