Boost logo

Boost Users :

Subject: Re: [Boost-users] [variant] variant member function apply_visitor( visitor ) thread-safe?
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-09-08 11:39:21


AMDG

Ovanes Markarian wrote:
> so you mean there are no VTable fakes inside as it is suggested by Andrei
> and in fact an additional numeric type identifier is stored? Which type from
> the type list is stored?
>

At the lowest level, boost::variant<std::string, int> (e.g.)
looks something like:

class variant {
    boost::aligned_storage<
        max(sizeof(std::string), sizeof(int)),
        lcm(alignment_of<std::string>::value, alignment_of<int>::value)>
buffer_;
    int which_;
};

apply_visitor is implemented something to the effect of

switch(which_) {
    case 0: return(f(*static_cast<std::string*>(buffer_.address())));
    case 1: return(f(*static_cast<int*>(buffer_.address())));
}

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net