std::bind with std::reference_wrapper<T> argument doesn't work on function object with template operator() - by Sven Groot [MSFT]

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


1
0
Sign in
to vote
ID 688731 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 9/15/2011 10:13:32 PM
Access Restriction Public
Moderator Decision Sent to Engineering Team for consideration

Description

In order to pass arguments by reference to the std::bind function, you use std::reference_wrapper<T>, typically by invoking std::ref.

However, if std::bind is used on a function object whose operator() is itself a template function, that template will be instantiated with std::reference_wrapper<T> rather than T itself, which is incorrect behavior.

For example, the following code does not compile:

#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 code does compile and work correctly on g++ 4.6.1, or if std::bind/std::ref is replaced with boost::bind/boost::ref.

A real-world example where this is a problem is the Boost.Random library. For example, the following does not work on VC++ (but does on g++):

boost::mt19337 engine;
boost::uniform_int<> distribution(0, 10);
auto func = std::bind(distribution, std::ref(engine));
Sign in to post a comment.
Posted by Microsoft on 9/21/2011 at 8:03 PM
Hi,

Thanks for reporting this bug. Yep, we've already fixed it in VC11.

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 Sven Groot [MSFT] on 9/18/2011 at 6:23 PM
I just tested this on the Visual Studio 11 Developer Preview and was unable to reproduce. It appears this issue has already been fixed. I apologize for wasting your time. :)
Posted by MS-Moderator09 [Feedback Moderator] on 9/16/2011 at 2:26 AM
Thank you for submitting feedback on Visual Studio 2010 and .NET Framework. Your issue has been routed to the appropriate VS development team for review. We will contact you if we require any additional information.
Posted by MS-Moderator01 on 9/15/2011 at 10:43 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)