开发者

boost::variant & STL containers - Am I doing it wrong?

I'm scratching my head on this one. I'm trying to use boost::variant to store a vector of values of which the type is to be determined at run time. The vector's internal type doesn't change once it is known. I am trying to create an interface where my code can add a value to the vector and later I would like to retrieve all the values as a vector. My code will not compile. Is my approach wrong? Should I be using polymorphism for this job? The code is as follows (does not compile):

template <typename T>
class record_value_visitor : public boost::static_visitor<void>
{
public:
    record_value_visitor(T v) : _val(v) {}

    void operator()(vector<T>& vec) const
    {
        vec.push_back(this->_val);
    }

private:
    T _val;
};


class monitor
{
public:
    monitor(int type)
    {

        switch (type) {
            case 1:
                this->_values = vector<int>();
                break;
            case 2:
                this->_values = vector<string>();
                break;
            default:
                throw new exception("Invalid type");
        }

        this->_type = type;
    }


    void add_value(int val) 
    { 
        if ( this->_type != 1 ) throw new runtime_error("Invalid type for add_value()");

        boost::apply_visitor( record_value_visitor<int>(val), this->_values);
    }
    void add_value(string val)
    { 
        if ( this->_type != 2 ) throw new runtime_error("Invalid type for add_value()"); 
        boost::apply_visitor( record_value_visitor<string>(val), this->_values);
    }

private:
    int _type;
    boost::variant< vector<int>, vector<double>, vector<string> > _values;
};


int main(int argc, char* argv[])
{
    monitor int_mon(1);
    monitor str_mon(2);

    int_mon.add_value(4);
    str_mon.add_value(string("foo"));


    return 0;
}

EDIT: Add Error Message

    1>c:\boost_1_46_1\boost\variant\variant.hpp(832): error C2664: 'void record_value_visitor<T>::operator ()(const std::vector<_Ty> &) const' : cannot convert parameter 1 from 'T1' to 'const std::vector<_Ty> &'
1>          with
1>          [
1>              T=int,
1>              _Ty=int
1>          ]
1>          and
1>          [
1>              _Ty=int
1>          ]
1>          Reason: cannot convert from 'T1' to 'const std::vector<_Ty>'
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(145) : see reference to function template instantiation 'void boost::detail::variant::invoke_visitor<Visitor>::internal_visit<T>(T &,int)' being compiled
1>          with
1>          [
1>              Visitor=const record_value_visitor<int>,
1>              T=T1
1>          ]
1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(173) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke_impl<Visitor,VoidPtrCV,T>(int,Visitor &,VoidPtrCV,T *,boost::mpl::false_)' being compiled
1>          with
1>          [
1>              Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>,
1>              VoidPtrCV=void *,
1>              T=T1
1>          ]
1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(256) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke<Visitor,VoidPtrCV,T1,NoBackupFlag>(int,Visitor &,VoidPtrCV,T *,NoBackupFlag,int)' being compiled
1>          with
1>          [
1>              Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>,
1>              VoidPtrCV=void *,
1>              NoBackupFlag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_,
1>              T=T1
1>          ]
1>          c:\boost_1_46_1\boost\variant\variant.hpp(1776) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl<first_which,first_step,Visitor,VoidPtrCV,boost::variant<T0_,T1,T2>::has_fallback_type_>(const int,const int,Visitor &,VoidPtrCV,boost::mpl::false_,NoBackupFlag,Which *,step0 *)' being compiled
1>          with
1>          [
1>              Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>,
1>              VoidPtrCV=void *,
1>              T0_=std::vector<int>,
1>              T1=std::vector<double>,
1>              T2=std::vector<std::string>,
1>              NoBackupFlag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_,
1>              Which=first_which,
1>              step0=first_step
1>          ]
1>          c:\boost_1_46_1\boost\variant\variant.hpp(1787) : see reference to function template instantiation 'void boost::variant<T0_,T1,T2>::internal_apply_visitor_impl<Visitor,void*>(int,int,Visitor &,VoidPtrCV)' being compiled
1>          with
1>          [
1>              T0_=std::vector<int>,
1>              T1=std::vector<double>,
1>              T2=std::vector<std::string>,
1>              Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>,
1>              VoidPtrCV=void *
1>          ]
1>          c:\boost_1_46_1\boost\variant\variant.hpp(1810) : see reference to function template instantiation 'void boost::variant<T0_,T1,T2>::internal_apply_visitor<boost::detail::variant::invoke_visitor<Visitor>>(boost::detail::variant::invoke_visitor<Visitor> &)' being compiled
1>          with
1>          [
1>              T0_=std::vector<int>,
1>              T1=std::vector<double>,
1>              T2=std::vector<std::string>,
1>              Visitor=const record_value_visitor<int>
1>          ]
1>          c:\boost_1_46_1\boost\variant\detail\apply_visitor_unary.hpp(76) : see reference to function template instantiation 'void boost::variant<T0_,T1,T2>::apply_visitor<const Visitor>(Visitor &)' being compiled
1>          with
1>          [
1>              T0_=std::vector<int>,
1>              T1=std::vector<double>,
1>              T2=std::vector<std::string>,
1>              Visitor=record_value_visitor<int>
1>          ]
1>          c:\documents and settings\g88791\my documents\visual studio 2010\projects\test_boost\test_boost.cpp(58) : see reference to function template instantiation 'void boost::apply_visitor<record_value_visitor<T>,boost::variant<T0_,T1,T2>>(const Visitor &,Visitable &)' being compiled
1>          with
1>          [
1>              T=int,
1>              T0_=std::vector<int>,
1>              T1=std::vector<double>,
1>              T2=std::vector<std::string>,
1>              Visitor=record_value_visitor<int>,
1>              Visitable=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>
1>          ]
1>c:\boost_1_46_1\boost\variant\variant.hpp(832): error C2664: 'void record_value_visitor<T>::operator ()(const std::vector<_Ty> &) const' : cannot convert parameter 1 from 'T2' to 'const std::vector<_Ty> &'
1>          with
1>          [
1>              T=int,
1>              _Ty=int
1>          ]
1>          and
1>          [
1>              _Ty=int
1>          ]
1>          Reason: cannot convert from 'T2' to 'const std::vector<_Ty>'
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(145) : see reference to function template instantiation 'void boost::detail::variant::invoke_visitor<Visitor>::internal_visit<T>(T &,int)' being compiled
1>          with
1>          [
1>              Visitor=const record_value_visitor<int>,
1>              T=T2
1>          ]
1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(173) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke_impl<Visitor,VoidPtrCV,T>(int,Visitor &,VoidPtrCV,T *,boost::mpl::false_)' being compiled
1>          with
1>          [
1>              Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>,
1>              VoidPtrCV=void *,
1>              T=T2
1>          ]
1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(256) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke<Visitor,VoidPtrCV,T2,NoBackupFlag>(int,Visitor &,VoidPtrCV,T *,NoBackupFlag,int)' being compiled
1>          with
1>          [
1>              Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>,
1>              VoidPtrCV=void *,
1>              NoBackupFlag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_,
1>              T=T2
1>          ]
1>c:\boost_1_46_1\boost\variant\variant.hpp(832): error C2664: 'void record_value_visitor<T>::operator ()(const std::vector<_Ty> &) const' : cannot convert parameter 1 from 'T0' to 'const std::vector<_Ty> &'
1>          with
1>          [
1>              T=std::string,
1>              _Ty=std::string
1>          ]
1>          and
1>          [
1>              _Ty=std::string
1>          ]
1>          Reason: cannot convert from 'T0' to 'const std::vector<_Ty>'
1>          with
1>          [
1>              _Ty=std::string
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(145) : see reference to function template instantiation 'void boost::detail::variant::invoke_visitor<Visitor>::internal_visit<T>(T &,int)' being compiled
1>          with
1>          [
1>              Visitor=const record_value_visitor<std::string>,
1>              T=T0
1>          ]
1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(173) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke_impl<Visitor,VoidPtrCV,T>(int,Visitor &,VoidPtrCV,T *,boost::mpl::false_)' being compiled
1>          with
1>          [
1>              Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>,
1>              VoidPtrCV=void *,
1>              T=T0
1>          ]
1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(256) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke<Visitor,VoidPtrCV,T0,NoBackupFlag>(int,Visitor &,VoidPtrCV,T *,NoBackupFlag,int)' being compiled
1>          with
1>          [
1>              Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>,
1>              VoidPtrCV=void *,
1>              NoBackupFlag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_,
1>              T=T0
1>          ]
1>          c:\boost_1_46_1\boost\variant\variant.hpp(1776) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl<first_which,first_step,Visitor,VoidPtrCV,boost::variant<T0_,T1,T2>::has_fallback_type_>(const int,const int,Visitor &,VoidPtrCV,boost::mpl::false_,NoBackupFlag,Which *,step0 *)' being compiled
1>          with
1>          [
1>              Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>,
1>              VoidPtrCV=void *,
1>              T0_=std::vector<int>,
1>              T1=std::vector<double>,
1>              T2=std::vector<std::string>,
1>              NoBackupFlag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_,
1>              Which=first_which,
1>              step0=first_step
1>          ]
1>          c:\boost_1_46_1\boost\variant\variant.hpp(1787) : see reference to function template instantiation 'void boost::variant<T0_,T1,T2>::internal_apply_visitor_impl<Visitor,void*>(int,int,Visitor &,VoidPtrCV)' being compiled
1>          with
1>          [
1>              T0_=std::vector<int>,
1>              T1=std::vector<double>,
1>              T2=std::vector<std::string>,
1>              Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>,
1>              VoidPtrCV=void *
1>          ]
1>          c:\boost_1_46_1\boost\variant\variant.hpp(1810) : see reference to function template instantiation 'void boost::variant<T0_,T1,T2>::internal_apply_visitor<boost::detail::variant::invoke_visitor<Visitor>>(boost::detail::variant::invoke_visitor<Visitor> &)' being compiled
1>          with
1>          [
1>              T0_=std::vector<int>,
1>              T1=std::vector<double>,
1>              T2=std::vector<std::string>,
1>              Visitor=const record_value_visitor<std::string>
1>          ]
1>          c:\boost_1_46_1\boost\variant\detail\apply_visitor_unary.hpp(76) : see reference to function template instantiation 'void boost::variant<T0_,T1,T2>::apply_visitor<const Visitor>(Visitor &)' being compiled
1>          with
1>          [
1>              T0_=std::vector<int>,
1>              T1=std::vector<double>,
1>              T2=std::vector<std::string>,
1>              Visitor=record_value_visitor<std::string>
1>          ]
1>          c:\documents and settings\g88791\my documents\visual studio 2010\projects\test_boost\test_boost.cpp(63) : see reference to function template instantiation 'void boost::apply_visitor<record_value_visitor<T>,boost::variant<T0_,T1,T2>>(const Visitor &,Visitable &)' being compiled
1>          with
1>          [
1>              T=std::string,
1>              T0_=std::vector<int>,
1>              T1=std::vector<double>,
1>              T2=std::vector<std::string>,
1>              Visitor=record_value_visitor<std::string>,
1>              Visitable=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>
1>          ]
1>c:\boost_1_46_1\boost\variant\variant.hpp(832): error C2664: 'void record_value_visitor<T>::operator ()(const std::vector<_Ty> &) const' : cannot convert parameter 1 from 'T1' to 'const std::vector<_Ty> &'
1>          with
1>          [
1>              T=std::string,
1>              _Ty=std::string
1>          ]
1>          and
1>          [
1>              _Ty=std::string
1>          ]
1>          Reason: cannot convert from 'T1' to 'const std::vector<_Ty>'
1>          with
1>          [
1>              _Ty=std::string
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(145) : see reference to function template instantiation 'void boost::detail::variant::invoke_visitor<Visitor>::internal_visit<T>(T &,int)' being compiled
1>          with
1>          [
1>              Visitor=const record_value_visitor<std::string>,
1>              T=T1
1>          ]
1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(173) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke_impl<Visitor,VoidPtrCV,T>(int,Visitor &,VoidPtrCV,T *,boost::mpl::开发者_如何学编程false_)' being compiled
1>          with
1>          [
1>              Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>,
1>              VoidPtrCV=void *,
1>              T=T1
1>          ]
1>          c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(256) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke<Visitor,VoidPtrCV,T1,NoBackupFlag>(int,Visitor &,VoidPtrCV,T *,NoBackupFlag,int)' being compiled
1>          with
1>          [
1>              Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>,
1>              VoidPtrCV=void *,
1>              NoBackupFlag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_,
1>              T=T1
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


The following compiles and runs as expected:

template<typename T>
struct record_value_visitor : boost::static_visitor<>
{
    explicit record_value_visitor(T const& val) : val_(val) { }

    void operator ()(std::vector<T>& vec) const
    {
        vec.push_back(this->val_);
    }

    template<typename U>
    void operator ()(std::vector<U> const&) const { /* noop */ }

private:
    T val_;
};

template<typename T>
record_value_visitor<T> make_record_value_visitor(T const& val)
{
    return record_value_visitor<T>(val);
}

struct monitor
{
    explicit monitor(int const type) : type_(type)
    {
        switch (type)
        {
        case 1:
            this->values_ = std::vector<int>();
            break;
        case 2:
            this->values_ = std::vector<std::string>();
            break;
        default:
            throw std::exception("Invalid type");
        }
    }

    void add_value(int const val)
    {
        if (this->type_ != 1)
            throw std::runtime_error("Invalid type for add_value()");
        boost::apply_visitor(make_record_value_visitor(val), this->values_);
    }

    void add_value(std::string const& val)
    {
        if (this->type_ != 2)
            throw std::runtime_error("Invalid type for add_value()");
        boost::apply_visitor(make_record_value_visitor(val), this->values_);
    }

private:
    int type_;
    boost::variant<
        std::vector<int>,
        std::vector<double>,
        std::vector<std::string>
    > values_;
};

int main()
{
    monitor int_mon(1);
    int_mon.add_value(4);

    monitor str_mon(2);
    str_mon.add_value("foo");
}

The visitor's operator() needs to be callable for every possible type in the variant – the templated record_value_visitor::operator() is the key here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜