CPP Templates 之 template 關鍵字的用法技巧

來源:互聯網
上載者:User
template 關鍵字的用法技巧

說明:

限定符包括:::,.,->

一、當類型名稱具有以下性質是,就應該在該名稱前面添加template首碼:
1.名稱出現在一個模板中
2.名稱是受限的
3.名稱不是用於指定基類繼承的列表中,也不是位於引入建構函式的成員初始化列表中
4.名稱依賴於模板

而且,只有當前面三個條件同時滿足時才能使用首碼template,例如:

template<typename T>

struct S:X<T>:Base{
    S():X<T>::Base(typename X<T>::Base(0)){}
    X<T> f(){//不能使用template
        template X<T>::C *p;//指標p的聲明
        X<T>::D *q;//乘積
    }
    typename X<int>::C * s;//可有可無,因為不符合第四條規定
};

struct U{
    X<int>::C * pc;//不能使用template,因為它並不是在模板中使用,U不是模板
};

二、如果一個模板名稱是依賴型名稱,需要在該名稱前插入template關鍵字

例如:

template<typename T,int N>
class Weird{
    public:
    void casel(template Shell<T>::template In<N>::template Deep<N> * p){
    
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.