Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6172207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:21:11+00:00 2026-05-23T23:21:11+00:00

I’m scratching my head on this one. I’m trying to use boost::variant to store

  • 0

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 ==========
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:21:12+00:00Added an answer on May 23, 2026 at 11:21 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.