模板类 的 typename 关键字

本文详细介绍了C++中typename关键字的使用场景,尤其是在模板定义中如何正确使用该关键字来声明类型。通过具体示例解释了在依赖于模板参数的类型名前加上typename的重要性。
 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值