|
Boost : |
From: Eric Niebler (eric_at_[hidden])
Date: 2005-12-17 13:17:34
Thorsten Ottosen wrote:
> Eric Niebler wrote:
>
>>Here's my latest attempt. This works on gcc 3.4.4 (cygwin), but not on
>>VC7.1 or (sadly) Comeau Online. Language lawyers, start your engines!
>
> Eric, this is very cool :-)
>
> I tried to simplify it a little:
>
> template<typename T>
> struct probe
> {
> operator T ();
> operator T&() const;
> };
>
> template<typename T>
> char is_volatile( const T& );
>
> template<typename T>
> double is_volatile( T& );
>
> That gave the same results.
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, and that in bar, T should be deduced at "foo const".
With gcc, it does *not* compile because the expression "true ?
conv<foo>() : cfoo()" appears to have the type "rvalue of foo" instead
of "rvalue of foo const". gcc is inadvertantly dropping cv-modifiers
where it shouldn't.
But we're certainly free to use that to our advantage. :-)
> Then I did
>
> > template<typename T>
> > probe<T> make_probe(T const &);
>
> template<typename T>
> const probe<T> make_probe(T const &);
>
> Now it works on vc7.1 too. Not como.
Whoa. If the probe is const-qualified, how can the non-const member ever
get called on it? Seems like you've found a compiler bug. I would test
with Comeau Online, but it's not responding at the moment.
Anyway, getting this to work with vc7.1 is a major advance. Thanks.
> I'm not a language specialist and my understanding of ?: comes mainly
> from your article. That said, I find the solution very intuitive.
If we can make this work on the popular compilers, it'll be a big win
for both boost.foreach and boost.typeof. It doesn't matter if we need to
take advantage of compiler bugs to get it. I wonder if other compilers
and other versions of gcc accept either my variant or yours.
-- 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