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
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:
I would use a boost::variant as follows:
Feel free to add more types to the variant as needed.