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 7083161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:08:38+00:00 2026-05-28T07:08:38+00:00

template<typename T> class ClassVariantVisitor : public boost::static_visitor<T> { public: T operator()(int& i) const {

  • 0
template<typename T>
class ClassVariantVisitor : public boost::static_visitor<T>
{
public:
  T operator()(int& i) const {
    try
      {
        return boost::lexical_cast<T>(i);
      } 
    catch ( boost::bad_lexical_cast& e)
      {
        throw e.what();
      }
  }

  T operator()(double& d) const {
    try
      {
        return boost::lexical_cast<T>(d);
      } 
    catch ( boost::bad_lexical_cast& e)
      {
        throw e.what();
      }
  }

  // ...
};

As you can see, for each different type, the implementation code for operator() is exactly same. Is there a practical way that we can simplify the code?

Thank you

// Updated based on the comments //

template<typename T>
class ClassVariantVisitor : public boost::static_visitor<T>
{
public:
  T operator()(T& i) const {
    try
      {
        return boost::lexical_cast<T>(i);
      } 
    catch ( boost::bad_lexical_cast& e)
      {
        throw e.what();
      }
  }
};

Then the compiler(G++) will generate tons of errors.

///// Updated 2 based on comments from iammilind

#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <boost/variant.hpp>
#include <boost/lexical_cast.hpp>

using namespace std;

typedef boost::variant<int, double, string> VarIntDoubleString;

// T is the result_type
template<typename T>
class ClassVariantVisitor : public boost::static_visitor<T>
{
public:
  template<typename U>
  T operator()(U& i) const {
    try
      {
        return boost::lexical_cast<T>(i);
      } 
    catch ( boost::bad_lexical_cast& e)
      {
        throw e.what();
      }
  }
};

int main(void)
{
  map<string, VarIntDoubleString> mapValuesThree;

  // store & retrieve char
  mapValuesThree["char_fieldI"] = VarIntDoubleString('c');
  char fieldI = boost::apply_visitor(ClassVariantVisitor<char>(), mapValuesThree["char_fieldI"]);
  cout << "fieldI: " << fieldI << endl;
}


~/Documents/C++/boost $ g++ -o p192f4 p192f4.cpp -Wall
~/Documents/C++/boost $ ./p192f4
terminate called after throwing an instance of 'char const*'
aborted
~/Documents/C++/boost $ g++ --version
g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2

To iammilind:
As you can see, the compiler doesn’t generate any errors or warning during the compilation time.

// Updated 3 based on comments from Konstantin Oznobihin

#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <boost/variant.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/contains.hpp>
#include <boost/utility/enable_if.hpp>

using namespace std;
template<typename T>
class ClassVariantVisitor : public boost::static_visitor<T>
{
public:
  typedef boost::mpl::vector<int, double, string> VarIntDoubleString;

  template <class U>
  typename boost::enable_if<
    typename boost::mpl::contains<VarIntDoubleString, U>::type, T>::type operator()(U &v) const 
  {
    try
      {
        return boost::lexical_cast<T>(v);
      } 
    catch ( boost::bad_lexical_cast& e)
      {
        throw e.what();
      }
  }
};

int main(void)
{
  map<string, ClassVariantVisitor::VarIntDoubleString> mapValuesThree;

  // store & retrieve double
  mapValuesThree["double_fieldJ"] = ClassVariantVisitor<double>::VarIntDoubleString(2.3456);
  double fieldJ = boost::apply_visitor(ClassVariantVisitor<double>(), mapValuesThree["double_fieldJ"]);
  cout << "fieldJ: " << fieldJ << endl;

}

I have no knowledge about boost::mpl and cannot make it work. Please refer to errors
May you let me how I can correct the code so that I use your idea and make it work.
Thank you

  • 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-28T07:08:38+00:00Added an answer on May 28, 2026 at 7:08 am

    Use templatized operator() as suggested by iammilind but filter types with boost::mpl:

    
    template<typename T>
    class ClassVariantVisitor : public boost::static_visitor<T>
    {
    public:
      typedef boost::mpl::vector<int, double> source_types;
    
      template <class U>
      typename boost::enable_if<
        typename boost::mpl::contains<source_types, U>::type,
        T
      >::type operator()(U &v) const {
        try
          {
            return boost::lexical_cast<T>(v);
          } 
        catch ( boost::bad_lexical_cast& e)
          {
            throw e.what();
          }
    };
    
    

    UPDATE:
    If you have boost::variant you can use it’s nested types sequence types instead of boost::mpl::vector and you don’t need to define the variant inside ClassVariantVisitor, here is an updated solution based on your code:

    
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <map>
    #include <boost/variant.hpp>
    #include <boost/lexical_cast.hpp>
    #include <boost/mpl/vector.hpp>
    #include <boost/mpl/contains.hpp>
    #include <boost/utility/enable_if.hpp>
    
    using namespace std;
    
    typedef boost::variant<int, double, string> VarIntDoubleString;
    
    template<typename T>
    class ClassVariantVisitor : public boost::static_visitor<T>
    {
    public:
      template <class U>
      typename boost::enable_if<
        typename boost::mpl::contains<VarIntDoubleString::types, U>::type, T>::type operator()(U &v) const 
      {
        try
          {
            return boost::lexical_cast<T>(v);
          } 
        catch ( boost::bad_lexical_cast& e)
          {
            throw e.what();
          }
      }
    };
    
    int main(void)
    {
      map<string, VarIntDoubleString> mapValuesThree;
    
      // store & retrieve double
      mapValuesThree["double_fieldJ"] = VarIntDoubleString(2.3456);
      double fieldJ = boost::apply_visitor(ClassVariantVisitor<double>(), mapValuesThree["double_fieldJ"]);
      cout << "fieldJ: " << fieldJ << endl;
    }
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

template <typename T> class Test { friend Test<T> & operator * (T lhs, const
I have a class template <typename Iterator, typename Value> class Foo { public: Foo(const
Because I've overloaded the operator++ for an iterator class template<typename T> typename list<T>::iterator& list<T>::iterator::operator++()
I have a class template <typename T> class C { static const int K=1;
template<typename T> class Base { protected: Base() {} T& get() { return t; }
template <typename Type> class Foo { Foo& Bar() { return *this; } }; Why
Consider: template <typename T> class Base { public: static const bool ZEROFILL = true;
The offending code: template<typename T> class SharedObject { public: typedef boost::intrusive_ptr<T> Pointer; typedef boost::intrusive_ptr<T
Given this code: class X { public: template< typename T > void func( const
This is my template matrix class: template<typename T> class Matrix { public: .... Matrix<T>

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.