开发者

Testing for String or other Data Type array using templates in C++

开发者 https://www.devze.com 2023-02-13 18:49 出处:网络
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 o

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号