为什么使用std::bind出现参数错误

why occurred parameter error using std::bind

本文关键字:参数 错误 bind std 为什么      更新时间:2023-10-16

我正在研究c++11规范

使用std::bind,我不明白为什么会出现错误

下面是简单的代码

class ClassInfo
{
public:
    ClassInfo() : nID(0) {}
    ~ClassInfo() {}
    bool GetInfo1(int nVal1, int& nOutVal1, std::string& strOut1, std::string& strOut2 )
    {
        nOutVal1 = nID;
        strOut1 = "out1";
        strOut2 = "out2";
        return true;
    }
    bool GetInfo2(int nVal1, int& nOutVal1, std::string& strOut1, std::string& strOut2, std::string& strOut3 )
    {
        nOutVal1 = nID;
        strOut1 = "out1";
        strOut2 = "out2";
        strOut3 = "out3";
        return true;
    }
    int nID;
};
int main()
{
    std::shared_ptr<ClassInfo> spInfo = std::make_shared<ClassInfo>();
    spInfo->nID = 10;
    int nVal1 = 5;
    int nOutVal1;
    std::string strOut1;
    std::string strOut2;
    std::string strOut3;
    auto _func1 = std::bind(&ClassInfo::GetInfo1, spInfo, nVal1, std::ref(nOutVal1), std::ref(strOut1), std::ref(strOut2));
    _func1();
    auto _func2 = std::bind(&ClassInfo::GetInfo2, spInfo, nVal1, std::ref(nOutVal1), std::ref(strOut1), std::ref(strOut2), std::ref(strOut3));
    _func2();
}

此代码未编译。。

auto _func1 = std::bind(&ClassInfo::GetInfo1, spInfo, nVal1, std::ref(nOutVal1), std::ref(strOut1), std::ref(strOut2));

这还可以,但是

auto _func2 = std::bind(&ClassInfo::GetInfo2, spInfo, nVal1, std::ref(nOutVal1), std::ref(strOut1), std::ref(strOut2), std::ref(strOut3));

此代码中发生编译错误。为什么会出现错误?

我解决了这个问题。此错误发生在visualstudio2012。视觉工作室2015是明确的。所以我解决了使用增强绑定