Using boost:::variant - what am I doing wrong? (SOLVED)

Started by
0 comments, last by garyfletcher 18 years, 9 months ago
I have a DLL that has a boost::variant type typedef boost::variant<int, std::string> argTypes; and I've got an argTypes declared argTypes myArgs; at runtime myArgs is populated with some value (int or std::string) and I'm trying to retrieve that value into an argument (argValue), who's type isn't known. I've got a template function that is attempting to get the value of args that matches the current type of argValue, but I'm having lots of problems getting that value. I've got:

typedef boost::variant< int, std::string, const char*, char*, float, double, 
                       unsigned int, unsigned char, long int > argTypes;

argTypes myArgs;

template < class aType >
bool mGetArgValue(aType& argValue)
{
  argValue= boost::get< aType >(myArgs);  
}





calling from my project that uses the DLL like

int arg1;
std::string arg2;
    
std::cout << "Get arg1" << std::endl;
    
if(!GE->mGetArgValue(arg1)
{
    std::cout << "Unable to get arg1" << std::endl;   
    return false; 
}
    
std::cout << arg1 << std::endl;
    
std::cout << "Get arg2" << std::endl;
    
if(!GE->mGetArgValue(arg2))
{
   std::cout << "Unable to get arg2" << std::endl;   
   return false; 
}
    
std::cout << "arg2 = " << arg2 << std::endl;





Everything seems to work if when I'm retrieving arg1 (int), but I get a problem when I try to get arg2 (std::string) and get the following DEV-C++ messagw runtime message to stderr: "This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information." I get this to stdout: Get arguments Get arg1 70 Get arg2 Where the arguements I put into args where: std::string aString = "arg1"; int anInt = 70; But from the boost documentation on boost:get<> I'm using it correctly? Anyone got any ideas? Do I need to use static_visitor<>, although I have tried this as well and get compile time errors in the project if I try to use myArgs as a return type like this:

class mGetArg : public boost::static_visitor< myArgs >
{
    public:
        template < typename T >
        T& operator()(T& operand1, T& operand2) const
        {
            operand2 = operand1;
                    
            return operand2;
        }

};

Do you need to explicitly define the return type when using a binary visitor, this seems to defeat the object a little.



??????? [Edited by - garyfletcher on June 30, 2005 5:41:38 PM]
Gary.Goodbye, and thanks for all the fish.
Advertisement
Sorted it. I was actually putting a const char* into my arguments list not a std::string...guess I need some exception handling in there.
Gary.Goodbye, and thanks for all the fish.

This topic is closed to new replies.

Advertisement