Evaluating function return type at run_time

closed account (SECMoG1T)
Hi all, am working on a console application and i really need some help,i made a class that totally fell out of design and am not willing to redo it, i just need a simple work around n all will be fine.
my question is : Is it possible to have a function whose return type will be evaluated at run-time, if possible how can it be done.

For example :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  struct data
   {
     std::string result;///contains numeric values only
     int extract_int() const {return stoi(result);}
     double extract_dbl() const {return stod(result);}
     /*type*/ get_results() const;///here
   };

   /*type*/ data::get_results() const
     {
        if(result.find('.')==std::string::npos)
          return extract_int();
        else
          return extract_dbl();
     }    


Thanks in advance.
Last edited on
closed account (SECMoG1T)
Is there a way i could do this, please?
closed account (SECMoG1T)
Guys please i need a way to do something like this or possibly an alternative because my function can only id the type to use based on the fact that a period is present in the variable result or not... i cant figure anything out (00) , Ill be very glad.
^
Last edited on
Use a discriminated union.
boost::variant could be used: http://www.boost.org/doc/libs/1_57_0/doc/html/variant.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <boost/variant.hpp>

boost::variant<int,double> to_number( std::string str )
{
    try
    {
        std::size_t pos = 0 ;
        // http://en.cppreference.com/w/cpp/string/basic_string/stol
        int value = std::stoi( str, std::addressof(pos), 0 ) ;
        if( pos == str.size() ) return value ;
    }
    catch( const std::exception& ) {}
    
    // http://en.cppreference.com/w/cpp/string/basic_string/stof
    return std::stod(str) ;
}

int main()
{
    struct print_it : boost::static_visitor<void>
    {
       void operator() ( int value ) const { std::cout << "int: " << value << '\n' ; }
       void operator() ( double value ) const { std::cout << "double: " << value << '\n' ; }
    };

    for( std::string str : { "1234", "1234.5", "-99", "0777", "0xff", "-0x1ff.7p-2", "inf", "-infinity", "NaN", "hello" } )
    {
        std::cout << str << " => " ;
        try
        {
            const auto var = to_number(str) ;
            boost::apply_visitor( print_it(), var ) ;
        }
        catch( const std::exception& ) { std::cerr << "**** error: no conversion could be performed\n" ; }
    }
}

http://coliru.stacked-crooked.com/a/6ff886d88deb85bd
closed account (SECMoG1T)
Ooh thank you very much @JLBorges though I haven't read about boost and can understand much of the snippet , however i'll have to use it for now "as it is" later I can catch up with boost, BTW am also not sure if my IDE have that library , i'll check it out , thank again for your help , i appreciate.
Last edited on
Topic archived. No new replies allowed.