Christopher Smith on 17 May 2003 04:20:01 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [ALACPP] Chris' stupid C++ trick of the week


On Fri, 2003-05-16 at 20:23, arlo@xxxxxxxxx wrote:
> > template<typename T>
> > T& doFoo(T& aTarget) {
> >     //do something with aTarget
> >     return aTarget;
> > }
> 
> Actually, upon looking at this code a bit more, I found a more problematic
> error. What if the template is specialized with int&?
> 
> The reference to reference problem is actually the one that the boost lib
> is trying to solve. However, the same mechanism (or one layering on top if
> it) can also solve the problem that you asked about.

It strikes me that the reference-to-reference problem is much less
likely to occur without a great deal of explicitness on the callers
part. That is the real source of my concern with the original problem:
the "surprise" factor. So, while you can create the problem you are
talking about with something on the order of:

bar = doFoo<MyClass&>(baz);

If you are going to explicitly instantiate template parameters on my
function, I expect you to understand how the template expands in a crazy
way.

Whereas, if you just do:

bar = doFoo(baz);

I can't imagine types for baz and bar which would cause the compiler to
select the double reference case.

--Chris
_______________________________________________
alacpp mailing list
alacpp@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/alacpp