Wednesday, 15 February 2012

c++ - Returning a different data type from function using a template -



c++ - Returning a different data type from function using a template -

i trying create function parse xml , homecoming either std::string or int using templates. have come next code:

template <class t> t queryxml(char *str) { if (typeid(t) == typeid(int)) homecoming evalulate_number(doc); else homecoming evaluate_string(doc); }

...and calling function this:

queryxml<int>("/people/name"); //i int returned queryxml<std::string>("/people/name"); //i string returned

but errors saying cannot convert pugi::string_t{aka std::basic_string<char>} int in return. there improve , cleaner way using templates? give thanks you!

template specialization.

// general form in algorithm utilize string... template <class t> t queryxml(char *str) { homecoming evaluate_string(doc); } // ...and want special behavior when using int template <> int queryxml(char *str) { homecoming evalulate_number(doc); }

c++ function templates return-value

No comments:

Post a Comment