Q:

Function call with a union return type

I have to use this library function in my code.  This function returns struct including a union something like this:
union  (
     int   int_value;
    double   double_value;
    const char *   text_value;
} value;

I have to check the return type with a switch statement like the example in the referred page everytime I call this function.  The real problem is
that I have to use this function more than once in my code, 15 times to be exact.  It is very very frustrating to check the return type every-time and the resulting code is very ugly if it's not mess.  I tried to wrap this code in a member function with return type auto.  But it did not even compile. 

How can I do this? 
What should be the correct way to do this? 

A:

Your example code will not fit a real world code! 
If you have a union which acts as a return type, you must have a idea which field of the union holds a valid value.  So I expect you have something like:
struct SOME_TYPE
{
    enum { IS_TEXT, IS_INT, IS_DOUBLE } valid_type;

    union return_val 
    {
        char* text_val;
        int int_val;
        double double_val;
    }
 };

And if your return type contains such a type info ( which is a must ) it is easy to create casting class which takes as argument this struct/union.  This class now can do any kind of tests for you, typesafe!  and automatically.  And it can also generate ( factory pattern ) subclasses, which can do all the actions which are only valid for the given value type. 
class UnionHandler
{
     public:
         UnionHandler( const SOME_TYPE& val )
         {
              switch ( val.valid_type) 
              {
                  case... whatever you want
              }
         }

     bool Check() { // whatever you want }

}

But at all: Avoid unions!!!!!!!! 