Returning a flexible datatype from a C++ function

Posted by GavinH on Programmers See other posts from Programmers or by GavinH
Published on 2011-02-07T23:09:15Z Indexed on 2011/02/07 23:33 UTC
Read the original article Hit count: 193

Filed under:

I'm developing for a legacy C++ application which uses ODBC for it's data access. Coming from a C# background, I really miss the ADO style of data access.

I'm writing a wrapper (because we can't actually use ADO) to make our data access less painful. This means no char arrays, no manual text blob streaming, and no declaritive column binding.

I'm struggling with how to store / return data values. In C# at least, you can declare an object and cast it to whatever (as long as the type is convertable).

My current C++ solution is to use boost::any to store the data value in a custom DataColumnValue object. This class has conversion and assignment operators to the various types used in our app (more than 10). There's a bit of complexity here because if you store an int in the boost::any and try to boost::any_cast<long> you get a boost::bad_any_cast. Client objects shouldn't have to know how the value is stored internally.

Does anyone have any experience trying to store / return values whose types are only known at runtime? Is there a better / cleaner way?

© Programmers or respective owner

Related posts about c++