|
Boost : |
From: Eric Niebler (eric_at_[hidden])
Date: 2005-12-20 17:29:26
Thorsten Ottosen wrote:
> Eric Niebler wrote:
>
>>Thorsten Ottosen wrote:
>>
>>>Eric Niebler wrote:
>>>
>>>>I noticed this too, and decided it was a bug in gcc. I think that given
>>>>
>>>> template<typename T> struct conv {
>>>> operator T() { return T(); }
>>>> };
>>>> template<typename T> void bar(T&) {}
>>>>
>>>>the expression:
>>>>
>>>> typedef foo const cfoo;
>>>> bar(true ? conv<foo>() : cfoo());
>>>>
>>>>should compile,
>>>
>>>Even though bar takes a T& and not const T& parameter?
>>
>>Yes. The type of (true ? conv<foo>() : cfoo()) is "rvalue of foo const."
>>And const rvalues successfully bind to T&, with T deduced as "foo const."
>
> well, if you call bar(1), it won't compile. what makes this different?
The type of 1 is "rvalue of int," not "rvalue of int const."
> I'm also confused by the fact that the above conversion operator can add
> a const to T
It's not adding a const. Look closely:
true ? conv<foo>() : cfoo()
^^^^
Now look above it: "typedef foo const cfoo". So the type of the 3rd
operand is "rvalue of foo const". The 2nd operand gets converted to
"rvalue of foo" and then picks up the "const" from the 3rd operand.
Make sense now?
-- Eric Niebler Boost Consulting www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk