Boost logo

Boost Users :

Subject: Re: [Boost-users] [variant] declaring recursive variants (Jerry Jeremiah)
From: ÇÇ־ǿ (qiaozhiqiang_at_[hidden])
Date: 2011-11-17 05:51:50


MS Visual Studio 2010 SP1 compiled OK.

But if use gcc, boost::variant<T> use boost:: sizeof_<T> and boost:: alignment_of <T>, and use sizeof(T), so need a complete type.
Maybe gcc bug or bug of boost for gcc ?
So you need workaround between // ADD THESE begin //////////// and // ADD THESE end ////////////, see next:

////////////////////////////////////////////
template<
      typename BOOST_MPL_AUX_NA_PARAM(T)
>
struct sizeof_
    : mpl::size_t< sizeof(T) >
{
    BOOST_MPL_AUX_LAMBDA_SUPPORT(1,sizeof_,(T))
};
./boost/variant/variant.hpp:1105:17: instantiated from 'boost::variant<std::basic_string<char>, long double, message_iter, config_iter>'
.\main.cpp:40:31: instantiated from here
./boost/mpl/sizeof.hpp:28:1: error: invalid application of 'sizeof' to incomplete type 'message_iter'
In file included from ./boost/aligned_storage.hpp:20:0,
                 from ./boost/variant/variant.hpp:41,
                 from ./boost/variant.hpp:17,
                 from .\main.cpp:8:
./boost/type_traits/alignment_of.hpp: In instantiation of 'const size_t boost::detail::alignment_of_impl<message_iter>::value':
////////////////////////////////////////////

#include <boost/variant.hpp>
#include <string>
#include <vector>
#include <list>
#include <map>

struct message_iter;
struct config_iter;

// ADD THESE begin ////////////
typedef std::map<std::string,int>::iterator any_map_iterator;

namespace boost {
namespace mpl
{

template<>
struct sizeof_<message_iter> : sizeof_<any_map_iterator>
{

};

template<>
struct sizeof_<config_iter> : sizeof_<any_map_iterator>
{

};

}

template<>
struct alignment_of<message_iter> : alignment_of<any_map_iterator>
{

};

template<>
struct alignment_of<config_iter> : alignment_of<any_map_iterator>
{

};

}

// ADD THESE end ////////////

typedef boost::variant<std::string
                      ,long double
                      ,message_iter
                      ,config_iter
> var;
typedef std::vector<var> value;
typedef std::map<std::string,value> message; typedef std::map<std::string,message> config;

struct config_iter
{
    config_iter(const config::iterator iter) : iter_(iter) {}
    operator config::iterator() const { return iter_; }
    config::iterator operator() () const { return iter_; }
    bool operator == (const config_iter & other) const
    {
        return iter_ == other.iter_;
    }
protected:
    config::iterator iter_;
};

struct message_iter
{
    message_iter(const message::iterator iter) : iter_(iter) {}
    operator message::iterator() const { return iter_; }
    message::iterator operator() () const { return iter_; }
    bool operator == (const message_iter & other) const
    {
        return iter_ == other.iter_;
    }
protected:
    message::iterator iter_;
};

// ADD THESE begin ////////////
BOOST_STATIC_ASSERT(sizeof(config_iter) == sizeof(message_iter));
BOOST_STATIC_ASSERT(sizeof(config_iter) == sizeof(any_map_iterator));
// ADD THESE end ////////////

struct identifier
{
    identifier()
    : name_(""), version_(0)
    {
        // std::cout << "identifier::identifier()" << std::endl;
    }
    identifier(const std::string & name, const value & version)
    : name_(name), version_(version)
    {
        // std::cout << "identifier::identifier(" << name << "," << version << ")" << std::endl;
    }

    bool operator == (const identifier & other) const
    {
        return name_ == other.name_ && version_ == other.version_;
    }
private:
    // updated by constructor
    std::string name_;
    value version_;
};

int main()
{
        return 0;
}

-----ÓʼþÔ­¼þ-----
·¢¼þÈË: Jerry Jeremiah [mailto:Jerry.Jeremiah_at_[hidden]]
·¢ËÍʱ¼ä: 2011Äê11ÔÂ17ÈÕ 17:33
ÊÕ¼þÈË: ÇÇ־ǿ
Ö÷Ìâ: RE: [variant] declaring recursive variants (Jerry Jeremiah)

Thanks for taking the time to look at it. I appreciate that.

If you read down a little farther you will see that I do that and it complains that config_iter isn't a complete type. And there is no way to get rid of that without removing all the "unrelated" classes. Can you figure out how to get the program right at the bottom to compile? Even adding the operator== to all the classes doesn't help...

Thanks for all the help,

Jerry


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