简体   繁体   中英

How to find out template parameter Type that STL container holds

For example vector<int> coll , holds int type objects. What if I don't know that coll is holds int (but I know it is vector), how would I go about finding type information ?

You can grab the information from the object's value_type :

using value_type = decltype(coll)::value_type;

static_assert(std::is_same<value_type, int>::value, "Type is not an int");

Use of the using alias and static_assert is available in C++11+

Though this can always be determined by using templates (which is more common):

template <class T>
void f(std::vector<T>& v); // use T as the type

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM