Tech Off Thread

8 posts

Visual C++ bug with std::bind

Back to Forum: Tech Off
  • User profile image
    Sven Groot

    I came across a bug with std::bind in Visual C++ 2010 SP1. The problem occurs in the following code:

    #include <iostream>
    #include <functional>
    using namespace std;
    
    struct Foo
    {
        typedef void result_type;
    
        template<typename T>
        void operator()(T &n)
        {
            n = 5;
        }
    };
    
    int main()
    {
        int n = 0;
        Foo f;
        auto func = bind(f, ref(n));
    
        func();
        cout << n << endl;
    
        return 0;
    }
    

    This should result in the template argument for Foo::operator() getting resolved as "int", but instead it gets resolved as "std::reference_wrapper<int>" which then fails to compile because there is no suitable operator= to do the assignment.

    The code does compile and run successfully on g++ 4.6.1, and using boost::bind/boost::ref instead of the std library versions also does the right thing. You can see the code working correctly here.

    I submitted a bug report, please vote on it if you can reproduce it.

  • User profile image
    Charles

    Thanks for the repro Sven. I perfectly-forwarded this thread to STL.

    C

  • User profile image
    Sven Groot

    I just tested this is VC11. It seems it has already been fixed (if only I'd had VC11 a few days earlier, then I wouldn't have had to file a pointless bug report). Smiley

  • User profile image
    Charles

    @Sven Groot: It's great to see that you are writing C++ and filing bugs (even if they're already fixed). Keep it up, Sven! Smiley
    C

  • User profile image
    STL

    bind() was a bug farm in VC9 SP1 and VC10 (<functional> is tricky in general, but bind() is the most complicated).  Our "faux variadics" rewrite is cleaning up the problems here - like this one - but more work remains to be done.  So if you run into other problems, please report them.

  • User profile image
    Sven Groot

    "Faux variadics" rewrite? Does this mean actual variadic templates aren't in the cards for VC11? Sad

  • User profile image
    STL
  • User profile image
    Sven Groot

    That is very, very disappointing. At this point most of the C++ code I write is for Linux, but I like developing in Visual Studio so I make sure it compiles there as well.

    If that list is accurate, it looks like I will have to ditch Visual Studio entirely (for C++) so I can use C++11 features that g++ already supports even today. Pity. Sad

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.