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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:20:13+00:00 2026-05-24T19:20:13+00:00

I want a simple variant with minimal overhead that can be passed to functions

  • 0

I want a simple variant with

  • minimal overhead
  • that can be passed to functions written in the C language

so I decided to use a std::vector like this

typedef std::vector<char> SimpleVariant;
SimpleVariant data;

(1) store a std::string

for(std::string::iterator it = str.begin(); it != str.end(); ++it)
    {
        data.push_back( *it );
    }
    data.push_back('\0');

(2) store a double

data.resize(64);
std::sprintf(&data[0], "%.*g", 20, val);

(3) get a string

std::string str = std::string( m_data.begin(), m_data.end() );

(4) get a double

double dbl = boost::lexical_cast<double>(&data[0]);

Is this a reasonable approach given my requirements? Is there lightweight variant that I can use instead of trying to reinvent the wheel?
I am aware of boost::variant and boost::any they are too heavyweight for my needs

  • 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-24T19:20:15+00:00Added an answer on May 24, 2026 at 7:20 pm

    You haven’t really given much information about how the C interface to this works. So this suggestion may not be helpful. But let’s assume you have a C interface that looks like this:

    int GetAValue(void *data, int size); //Returns the type of data stored in the data pointer. Will not write past size.
    void DoSomethingWithValue(int type, void *data, int size); //Gives data to C, to do something with. Does not modify it.
    void ModifyValue(int type, void *data, int size); //Gives data to C, where it modifies it.
    

    I would use a boost::variant as follows:

    typedef boost::variant<int, double, std::string> MyVariant;
    
    struct DoSomethingWithValueVisit : public boost::static_visitor<>
    {
      void operator()(int val)
      {
        DoSomethingWithValue(TYPE_INTEGER, &val, sizeof(int));
      }
    
      void operator()(double val)
      {
        DoSomethingWithValue(TYPE_DOUBLE, &val, sizeof(double));
      }
    
      void operator()(const std::string &val)
      {
        DoSomethingWithValue(TYPE_STRING, (void*)val.c_str(), val.size() + 1);
      }
    };
    
    struct ModifyValueVisit : public boost::static_visitor<>
    {
      void operator()(int &val)
      {
        ModifyValue(TYPE_INTEGER, &val, sizeof(int));
      }
    
      void operator()(double &val)
      {
        ModifyValue(TYPE_DOUBLE, &val, sizeof(double));
      }
    
      void operator()(std::string &val)
      {
        char buffer[128];
        strncpy(buffer, val.c_str(), 127);
        ModifyValue(TYPE_STRING, buffer, 128);
        val = buffer;
      }
    };
    
    MyVariant GetAValueFromC()
    {
      char buffer[128];
      int type = GetAValue(buffer, 128);
    
      switch(type)
      {
      case TYPE_INTEGER:
        return *reinterpret_cast<int*>(&buffer[0]);
      case TYPE_DOUBLE:
        return *reinterpret_cast<double*>(&buffer[0]);
      case TYPE_STRING:
        return std::string(buffer);
      }
    }
    
    int main()
    {
      MyVariant value = GetAValueFromC();
      //Non-modifying
      boost::apply_visitor(DoSomethingWithValueVisit(), value);
      //Modifying
      boost::apply_visitor(ModifyValueVisit(), value);
    }
    

    Feel free to add more types to the variant as needed.

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

Sidebar

Related Questions

I want a simple function that receives a string and returns an array of
I'm looking for a C++ container that's a cross between boost::array, boost::scoped_array and std::vector.
I want to create a function that can take different types of iterators which
I want to ask because I find that strange: the way varint is written
I want executed this simple curl operation through php how can i execute this
I have a huge table and I want simple sorting. It could be so
I want something simple in order to experiment/hack. I've created a lot interpreters/compilers for
I want a simple TextView to behave the way simple_list_item_1 in a ListView does.
I want a simple form with one text box and a submit button. For
I want a simple sign in app in which a user logins successfully if

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.