Boost logo

Boost :

From: Jonathan Graehl (jonathan_at_[hidden])
Date: 2004-06-04 16:51:16


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);
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk