Subject: [Boost-bugs] [Boost C++ Libraries] #2128: Add the ability to implicitly traverse reference wrappers
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2008-07-18 19:12:47
#2128: Add the ability to implicitly traverse reference wrappers
----------------------------------------------------+-----------------------
Reporter: mgoldshteyn_at_[hidden] | Owner: pdimov
Type: Feature Requests | Status: new
Milestone: Boost 1.36.0 | Component: bind
Version: Boost 1.35.0 | Severity: Problem
Keywords: implicitly traverse reference_wrappers |
----------------------------------------------------+-----------------------
Please add the following to boost:
namespace boost
{
template<class T> T* get_pointer( reference_wrapper<T> const & r )
{
return r.get_pointer();
}
}
----
Code demonstrating the issue:
#include <boost/bind.hpp>
#include <boost/ref.hpp>
#include <string>
// Define a simple class for our example class Crayon {
public:
explicit Crayon(const std::string &color=std::string()):m_color(color)
{}
const std::string &GetColor() const {return m_color;}
private:
std::string m_color;
};
int main(int argc, char *argv[])
{
using namespace std;
using namespace boost;
Crayon cr("blue");
boost::reference_wrapper<const Crayon> crefCr(cr);
// This doesn't compile, however, even though the reference_wrapper
class template has
// an overloaded operator T& () const function
std::string
colorFromConstRef2(boost::bind(&Crayon::GetColor,_1)(crefCr));
}
----
Peter Dimov's reply:
It doesn't because of the mem_fn behavior that bind inherits. The argument
is not a reference to Crayon and so is assumed to be a smart pointer to
Crayon. mem_fn then tries to invoke get_pointer() on it to obtain a
Crayon*.
You should be able to make it compile by adding a get_pointer overload for
reference_wrapper:
namespace boost
{
template<class T> T* get_pointer( reference_wrapper<T> const & r )
{
return r.get_pointer();
}
}
-- Ticket URL: <http://svn.boost.org/trac/boost/ticket/2128> Boost C++ Libraries <http://www.boost.org/> Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:49:58 UTC