Boost logo

Boost :

From: Pavol Droba (droba_at_[hidden])
Date: 2003-12-23 03:37:52


Hi,

I've been using boost::variant in my project. Recently I have encountered a problem,
that took me quite a while to track down.

Problem was quite trivial. I'm using variant's get<> function to retrieve a value from a
variant.
It' signature is as follows:

template<typename U, typename T1, typename T2, ..., typename TN>
  U & get(variant<T1, T2, ..., TN> & operand);
template<typename U, typename T1, typename T2, ..., typename TN>
  U & get(const variant<T1, T2, ..., TN> & operand);

My code was something like this:

std::string foo( const variant<std::string,int>& V )
{
   return boost::get<std::string>(V);
}

An exception was fired upon in the get<> function. I didn't know why, until I have find out,
that I need to specify <const std::string> as a get type.
This was not at all obvious to me. (Now it is, but I lost few hours to find it out).

It is clear, that the second overload of get<> cannot handle any non-const type as a U parameter.
I think, it would make sense to automaticaly add const specifier to the return value.
So the get<> would change to:

template<typename U, typename T1, typename T2, ..., typename TN>
  add_const<U>::type & get(const variant<T1, T2, ..., TN> & operand);

Am I missing something?

Regards,

Pavol


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