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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:56:54+00:00 2026-05-29T19:56:54+00:00

I want to construct boost::variant s containing default-constructed values, specified with a type index

  • 0

I want to construct boost::variants containing default-constructed values, specified with a type index – without writing my own switch statement over the type index.

I figure this must be possible, somehow, with MPL?

To clarify though, the index is not a compile-time constant expression.

The use case is that I need to construct a variant which will later be replaced with one containing the correct value, but at this point I only know the type index. Think of it as a lazy deserialisation problem.

  • 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-29T19:56:55+00:00Added an answer on May 29, 2026 at 7:56 pm

    You need to use the variant::types typedef. This gives you an MPL compatible sequence which we can then use with mpl::at and a template to do our bidding. This does the trick:

    #include <string>
    #include <boost/variant.hpp>
    #include <boost/mpl/at.hpp>
    #include <boost/mpl/int.hpp>
    
    template<typename U, typename V>
    void construct_in(V& v) {
      v = U();
      // modern
      // v = U{};
    }
    
    int main()
    {
      typedef boost::variant<int, std::string> variant;
      typedef boost::mpl::at<variant::types, boost::mpl::int_<1>>::type pos;
      variant v;
      // use type deduction
      construct_in<pos>(v);
      // does not throw, does work
      std::string& s =boost::get<std::string>(v);
      return 0;
    }
    

    Here goes the runtime-variant:

    #include <string>
    #include <vector>
    #include <functional>
    
    #include <boost/variant.hpp>
    #include <boost/mpl/at.hpp>
    #include <boost/mpl/int.hpp>
    #include <boost/mpl/for_each.hpp>
    
    typedef boost::variant<int, std::string> variant;
    typedef variant::types types;
    typedef std::vector< std::function<void(variant&)> > fvec;
    
    template<typename U, typename V>
    void construct_in(V& v) {
      v = U{};
    }
    
    struct build_and_add {
      fvec* funcs;
      template<typename T>
      void operator()(T) {
        funcs->push_back(&construct_in<T, variant>);
      }
    };
    
    
    int main()
    {
    
      variant v;
      std::vector< std::function<void(variant&)> > funcs;
    
      // cannot use a lambda, would need to be polymorphic
      build_and_add f = {&funcs};
      boost::mpl::for_each<types>(f);
    
      // this is runtime!
      int i = 1;
    
      funcs[i](v);
      // does not throw, does work
      std::string& s =boost::get<std::string>(v);
      return 0;
    }
    

    It’s a little arcane and will need some tweaking with variadic
    arguments to be truly generic, but it does what you want. Someone else needs to figure out if this results in significant
    code blow-up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to construct a transact sql script that will stop specified people from
I want to send an array constructed in javascript with the selected values of
I used boost::interprocess::managed_(windows_)shared_memory::construct to construct an interprocess vector holding an own class, which has
why do we need a generic type specified in Boost library? Isn't template enough?
I am using boost recursive variant to store the variant data which I want
I am currently writing a CUDA application and want to use the boost::program_options library
I want to convert a boost::posix_time::ptime to a boost::local_time::local_date_time using a timezone without daylight
If I want to construct a std::string with a line like: std::string my_string(a\0b); Where
In a Windows batch file, I want to construct a classpath of .jar files
i want to realize a construct in MS SQL that would look like this

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.