我有以下代码
int main()
{
cout << "Please enter your name..." << endl;
cin >> name;
cout << "Data type = " << typeid(name).name() << endl;
cin.get();
return 0;
}
根据我读过的关于 typeid 运算符的各种教科书和文档,我应该会读到
"Data type = string"
作为输出。相反,我得到以下信息
class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
有人知道我哪里出错了吗? FWIW,我使用的是 Visual Studio 2010 Professional。
最佳答案
没有错。
那些教科书首先应该告诉你 name()
的结果是实现定义的,很可能是 ""
。其次,该类型是 std::string
。 std::string
类型只是 std::basic_string
与 char
和 friend 的 typedef。
关于c++ - C++ 中的 typeid 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3721078/