Using std::tr1::bind with std::vector::push_back

后端 未结 4 847
孤独总比滥情好
孤独总比滥情好 2021-01-21 15:39

Why my VS2010 can\'t compile this code:

#include 
#include 
int main()
{
    std::vector vec;
    std::bind(&std::         


        
4条回答
  •  灰色年华
    2021-01-21 16:24

    You should be more specific why this doesn't seem to work for you.

    #include 
    #include 
    #include 
    
    int main(int argc, char* argv[]) {
        std::vector vec;
        std::tr1::bind(&std::vector::push_back, std::tr1::ref(vec), 1)();
        std::cout << "vec.size = " << vec.size() << std::endl;
        std::cout << "vec[0] = " << vec[0] << std::endl;
        return 0;
    }
    
    $ gcc -o test -lstdc++ test.cpp && ./test
    vec.size = 1
    vec[0] = 1
    

    Update: Luc Danton is right, the issue here is the overloaded push_back. See question Are there boost::bind issues with VS2010 ?. Also, note that the issue is not limited to push_back, see Visual Studio 2010 and boost::bind.

提交回复
热议问题