Tells the compiler that an unknown identifier is a type.
typename identifier;
Use this keyword only in template definitions.
This keyword must be used if the name is a qualified name dependent on a template argument; it is optional if the qualified name is not dependent.
例如:
// typename.cpp
template<class T> class X
{
typename T::Y m_y; // treat Y as a type
};
例如下面的程序,不加typename 关键字会编译错误。
//-------------------------------------------------------------------
#include <QtCore/QCoreApplication>
#include <map>
template <class T>
class MySetting
{
public:
void function();
private:
T m_data;
};
//////////////////////////////////////////////////////////////////////////
template <class T>
void MySetting<T>::function()
{
typename std::map<int, T>::iterator myit; //此处要用 typename 否则qt的mingw编译提示出错。vs2008不出错
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::map<int, int>::iterator myit;
return a.exec();
}
本文详细介绍了C++中typename关键字的使用场景,尤其是在模板定义中如何正确使用该关键字来声明类型。通过具体示例解释了在依赖于模板参数的类型名前加上typename的重要性。
786

被折叠的 条评论
为什么被折叠?



