Home Dashboard Directory Help
Search

Problem with non-const operator() and bind by Leigh Johnston


Status: 

Closed
 as Fixed Help for as Fixed


1
0
Sign in
to vote
Type: Bug
ID: 540098
Opened: 3/8/2010 9:25:07 AM
Access Restriction: Public
1
Workaround(s)
view
2
User(s) can reproduce this bug

Description

The following does not compile in VS2008 and (apparently) VS2010

#include <functional>

struct foo
{
void operator()() { }
};

int main()
{
foo o;
std::tr1::bind(o)();
}
Details
Sign in to post a comment.
Posted by Microsoft on 6/22/2010 at 2:19 PM
Hi,

Thanks again for reporting this bug. We've fixed it, and the fix will be available in VC11.

As always, if you have any further questions, feel free to E-mail me at stl@microsoft.com .

Stephan T. Lavavej
Visual C++ Libraries Developer
Posted by Microsoft on 3/9/2010 at 8:12 PM
Hi,

Thanks for reporting this bug. We believed that we had fixed this in VC9 SP1, but we missed the zero-argument case. Although it's too late to fix this in VC10, we're keeping this bug active so that it can be fixed in a future release.

Some additional explanation: We simulate variadic templates with macros. This works well for things like make_shared<T>(), but multiplies <functional>'s inherent complexity, and bind() is the most severely affected. That's one of the reasons why apparently simple bind() bugs can require very complicated fixes.

We have a regression test to ensure that the one-argument case works, although more complicated uses of bind() may trigger other bugs. (Fortunately, while VC10's bind() implementation is still fragile, lambdas supersede 99% of bind() uses, and the remaining 1% are better dealt with using named functors.) Here's an example:

C:\Temp>type meow.cpp
#include <functional>
#include <iostream>
#include <ostream>
using namespace std;
using namespace std::tr1;

struct Functor {
    typedef int result_type;

    int operator()(int x) { // NON-const
        return x * 2;
    }
};

int main() {
    Functor f; // NON-const

    int r = bind(f, 123)();

    cout << "This should be 246: " << r << endl;
}

C:\Temp>cl /EHsc /nologo /W4 meow.cpp
meow.cpp

C:\Temp>meow
This should be 246: 246

If you have any further questions, feel free to E-mail me at stl@microsoft.com .

Stephan T. Lavavej
Visual C++ Libraries Developer
Posted by Microsoft on 3/8/2010 at 9:51 PM

Thanks for your feedback.

We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution.
These specialized experts will follow-up with your issue.
Posted by Microsoft on 3/8/2010 at 7:04 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.
Posted by Fredrick_V on 3/8/2010 at 1:29 PM
Modify xxbind1.
Add a non-const version of _Bind0::_ApplyX that is exactly like the existing const version, only with the const qualification removed:

    template<class _Ret,
        _CLASS_BARG0>
        _Ret _ApplyX(_BARG0_B0)
        {    // call with no arguments
        return (_Callee.template _ApplyX<_Ret>());
        }