提高::绑定,性病::绑定和重载函数 [英] boost::bind, std::bind and overloaded functions

查看:150
本文介绍了提高::绑定,性病::绑定和重载函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到的boost ::绑定,不同的std ::绑定,可以与重载函数工作,当这些函数没有任何参数。我对吗?这是记录?

 的#include<升压/ bind.hpp>的#include<功能>
#包括LT&;&iostream的GT;无效美孚()
{
    性病::法院LT&;< :: foo的()\\ n;
}无效美孚(INT)
{
    性病::法院LT&;< :: foo的(INT)\\ n;
}诠释的main()
{
    提高::绑定(富)(); // 好
    提高::绑定(富,0)(); // 好    //的std ::绑定(富)(); //错误
    //的std ::绑定(富,0)(); //错误
}#包括LT&;升压/ bind.hpp>的#include<功能>
#包括LT&;&iostream的GT;无效美孚(INT)
{
    性病::法院LT&;< :: foo的(INT)\\ n;
}无效美孚(常量标准::字符串&安培;)
{
    性病::法院LT&;< :: foo的(常量标准::字符串&安培;)\\ n;
}诠释的main()
{
    //提高::绑定(富,0)(); //错误
    //提高::绑定(富,STR)(); //错误    //的std ::绑定(富,0)(); //错误
    //的std ::绑定(富,STR)(); //错误
}


解决方案

这在一定程度记录为接口的一部分 - >剧情简介部分,其中重载上市。正如你可以看到,升压做的的使用可变参数模板,因此给出的过载时,明确这些:

 模板<类R>未指定-2绑定(R(* F)());
模板< F级,A1级>未指定-3-1绑定(F楼A1 A1);
模板< R级,B1级,A1级>未指定-4绑定(R(* F)(B1),A1 A1);

编译器prefers重载版本用空的参数列表对他人的,因为它是一个更好的匹配。我不认为这是故意的,但。

I noticed that boost::bind, unlike std::bind, can work with overloaded functions when one of these functions doesn't have any parameters. Am I right? Is this documented?

#include <boost/bind.hpp>

#include <functional>
#include <iostream>

void foo()
{
    std::cout << "::foo() \n";
}

void foo(int)
{
    std::cout << "::foo(int) \n";
}

int main()
{
    boost::bind(foo)(); // Ok
    boost::bind(foo, 0)(); // Ok

    // std::bind(foo)(); // Error
    // std::bind(foo, 0)(); // Error
}

#include <boost/bind.hpp>

#include <functional>
#include <iostream>

void foo(int)
{
    std::cout << "::foo(int) \n";
}

void foo(const std::string&)
{
    std::cout << "::foo(const std::string&) \n";
}

int main()
{
    // boost::bind(foo, 0)(); // Error
    // boost::bind(foo, "str")(); // Error

    // std::bind(foo, 0)(); // Error
    // std::bind(foo, "str")(); // Error
}

解决方案

It is somewhat documented as part of the "Interfaces" -> "Synopsis" part, where the overloads are listed. As you can see there, Boost does not use variadic templates, hence when given those overload, explicitly these:

template<class R> unspecified-2 bind(R (*f) ());
template<class F, class A1> unspecified-3-1 bind(F f, A1 a1);
template<class R, class B1, class A1> unspecified-4 bind(R (*f) (B1), A1 a1);

the compiler prefers the overloaded versions with an empty argument list over others as it is a better match. I don't think this was intentional, though.

这篇关于提高::绑定,性病::绑定和重载函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆