Boost logo

Boost :

From: David B. Held (dheld_at_[hidden])
Date: 2003-03-18 12:28:21


"David B. Held" <dheld_at_[hidden]> wrote in message
news:b57jvh$bts$1_at_main.gmane.org...
> "David Abrahams" <dave_at_[hidden]> wrote in message
> news:u7kaw7h88.fsf_at_boost-consulting.com...
> > [...]
> > That's a pretty major problem, though. Your idea also cuts off
> > implicit conversions.
>
> Do you mean user-defined conversions, because it consumes one
> from by_ref->T? It's more awkward, but wouldn't calling a named
> member to get T solve that?
> [...]

I wonder if you mean the implicit conversion from T to by_ref in the
first place instead. It turns out that I really don't understand how
argument deduction goes, because Comeau doesn't like this code,
which I expected to work:

template <typename T, bool Big = (sizeof(T) > 8)>
class by_ref
{
public:
  by_ref(T const& val) : val_(&val) { }
  operator T const&() const { return *val_; }
private:
  T* val_;
};

template <typename T>
class by_ref<T, false>
{
public:
  by_ref(T val) : val_(val) { }
  operator T() const { return val_; }
private:
  T val_;
};

void g(long l) { }

template <typename T1>
void f(by_ref<T1> v1)
{
  g(v1);
}

int main()
{
  f(5);
}

"ComeauTest.c", line 31: error: no instance of function template "f"
          matches the argument list
            The argument types that you used are: (int)

Also, I was suprised that I had to put parens around the sizeof(T) > 8
expression, as I thought that the parser had to try to match the largest
possible token sequence to the template parameter. But maybe that
only applies for instantiations, and not definitions?

Dave


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