I am creating an array template class that needs to be able to handle as broad an input as possible. It will work no problem with int, float, char but I would like to be able to test for strings and other types of arrays so I can sort the main array. I would like the functions to be as generic as possible so it deals with everything the same way.
Essentially, I want to know how to test an 开发者_C百科unknown variable for its data type in templates.
The technique you search for is called "Traits". With a trait you determine the type of a variable by using template specialization.
See http://accu.org/index.php/journals/442 for a good explanation with sample code.
You might be interested in using Boost.TypeTraits
If you want to sort your array, just use std::sort. Why do you think you need to treat std::string different from an int? They mostly work the same.
精彩评论