boost::ref implicit conversion operator overload problems

If I pass a boost::ref to a template function that calls operator() with one argument, the conversion to T& isn't used to find matching functions. The problem seems to also exist for other overloaded operators. What should I do in this case? I thought the idea was for the ref to be transparent to the template code you pass it to. As a stopgap, I wrote template functions "deref" that must be applied at the calling site (these just cast using unwrap_reference). This code works, but if you remove the "deref", doesn't compile under GCC or visual c++: #include <boost/ref.hpp> #include <cassert> template <class T> inline typename boost::unwrap_reference<T>::type & deref(T& t) { return t; } template <class T> inline const typename boost::unwrap_reference<T>::type & deref(const T& t) { return t; } struct Setter { int i; void operator()(int j) { i=j; } Setter() : i(0) {} Setter(int j) : i(j) {} }; template <class F> void apply1(F f) { deref(f)(1); } int main() { Setter s; apply1(s); assert(s.i == 0); apply1(boost::ref(s)); assert(s.i == 1); }

On Fri, 4 Jun 2004, Jonathan Graehl wrote:
If I pass a boost::ref to a template function that calls operator() with one argument, the conversion to T& isn't used to find matching functions. The problem seems to also exist for other overloaded operators.
boost::ref is supposed to do this, but nobody has gotten around to finishing it yet. I hope to get there for the next Boost release (whenever that will be).
What should I do in this case? I thought the idea was for the ref to be transparent to the template code you pass it to.
You can use, e.g., bind(ref(f), _1, _2) for now. Doug
participants (2)
-
Douglas Paul Gregor
-
Jonathan Graehl