为什么使用std :: bind

时间:2016-04-30 09:29:02

标签: c++ visual-studio-2012 bind

我正在研究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));

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

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。 这个错误发生在visual studio 2012上。 视觉工作室2015很清楚。所以我决定使用boost bind