jeudi 21 septembre 2017

Deduce return type of a function dynamically

I have a few functions defined which have return types of void, int, float, float*, etc(some classes aswell).

My cmd is a vector of strings inputed from user. where 0th location is the function name(read_lib, square, open_file) and the 1st location is the argument(/path/to/file , number_to_square) etc.

auto find_and_execute(vector<string> cmd){
//for(auto x: cmd){cout << x << endl;}
if(cmd.at(0) == "square") {return square(stoi(cmd.at(1)));} // unsigned_int
if(cmd.at(0) == "cube") {return cube(stoi(cmd.at(1)));}     // unsigned_int
if(cmd.at(0) == "open_file") {open_file(cmd.at(1));}        //void
if(cmd.at(0) == "read_lib") {read_lib(cmd.at(1));}          //void
if(cmd.at(0) == "read_verilog") {read_verilog(cmd.at(1));}  //void
if(cmd.at(0) == "set_top") {set_top(cmd.at(1));}            //void
if(cmd.at(0) == "get_pin") {return get_pin(cmd.at(1));}     // Class object
}

Error: inconsistent deduction for 'auto': 'unsigned int' and then 'Pin'

Is there a better way than if-else. something like map(string to function name called with arguments)

Aucun commentaire:

Enregistrer un commentaire