Boost logo

Boost :

From: Gennadiy Rozental (gennadiy.rozental_at_[hidden])
Date: 2003-04-03 03:01:08


> > template<typename T>
> > void foo( T const& )
> > {
> > ....
> > }
> >
> > int main()
> > {
> > boost::variant<int,.....> v = 5;
> >
> > // Here I want to pass const reference to integer value of variant to
> > function foo
> > // foo( get<int>( v ) ); - type T is incorrect
> > foo( ??? );
> > }
>
> I don't see why this wouldn't work. What is "incorrect" regarding type T?

Try to compile and run this:
#include <iostream>

template<typename T>
void
foo( T const& )
{
   std::cout << typeid(T).name() << std::endl;
}

template<typename T>
struct get {
    operator T&() { return m_t; }

    T m_t;
};

int main()
{
   foo( get<int>() );
}

> I see it as the difference between dynamic_cast with a reference type
versus
> a pointer type. That is, the reference-based dynamic_cast throws, which as
> you note, works fine. But the pointer-based dynamic_cast provides a
> non-throwing mechanism as well (returns a null pointer). Thus,
> extract::check is the non-throwing analogue for extract.

I don't argue that it may be used. I argue that it will be rarely used. In
most cases when you access your variant you either know what type it holds
by inspecting it's which() result or will use some kind of visitation. You
may provide this form of value access but only as an addition to free form
one.

Gennadiy.


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