Have you looked at boost::variant? You can use operator<< and ostringstream to convert to string. Use something this: #include <boost/variant.hpp> #include <string> #include <iostream> #include <sstream> typedef boost::variant<bool,unsigned char,int,std::string> my_variant; int main() { unsigned char u2 = 2; my_variant mv1(u2); int i2 = 2; my_variant mv2(i2); my_variant mv3(std::string("3")); std::ostringstream oss (std::ostringstream::out); oss << mv1; std::cout << oss.str(); oss << mv2; std::cout << oss.str(); oss << mv3; std::cout << oss.str(); } Chris On 5/10/06, Rush Manbert <rush@manbert.com> wrote:
I have a template class that is designed to contain built in types or std::string types. I always need to be able to cast an instance of the class as a std::string, and I also need to be able to cast it as the parameter type with which it was instantiated.
In my class definition, I have defined two cast operators, like so: (Extraneous stuff omitted)
template<typename T> class MyDataObject : public MyDataObjectBase {
// Cast as std::string inline operator const std::string & () { ...some code here }
// Cast as T inline operator const T & () { ...some code here } }
The original code was written on a Mac and compiled with Gnu, which did not complain. even when I did this:
class MyStringObjectClass : public MyDataObject<std::string> { }
Now, however, I have moved the code to Windows, and the Visual Studio compiler complains about the second cast operator when I define MyStringObjectClass.
I thought that if I could conditionally define the cast as T operator (or the cast as std::string) I would be okay, but that means testing against the value of T with the preprocessor. I don't see how to do that, but I know that some of you folks are really good at bending the preprocessor to your will.
Can anyone show me a way out of this predicament?
Thanks, Rush _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users