模板类 的 typename 关键字

来源:互联网 发布:标书制作软件破解版 编辑:程序博客网 时间:2024/05/24 03:37
 

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();
}

 

原创粉丝点击