我有时会阅读有关基于 typeid 在运行时进行类型确定的信息,我尝试使用以下代码
#include <iostream>
#include <vector>
#include <typeinfo>
typedef std::vector<int> Vector;
template <class T> void foo(T &v)
{
cout << typeid(Vector::value_type).name() << endl; // this is ok
cout << typeid(T::value_type).name() << endl; // this doesn't compile
}
void main(void)
{Vector v;
foo(v);
}
上面的代码只有在我们将特定类型放入 typeid 时才能编译,但如果我使用模板 T 代替,它就不起作用。那么如果我有一个容器v,如何确定运行时的值类型呢?谢谢。