日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用using与typedef来定义别名

發布時間:2024/4/18 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用using与typedef来定义别名 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用using與typedef來定義別名

在C++中,using與typedef這兩個關鍵詞是大家用的比較多的,using關鍵詞用的最多的是using namespace的搭配如using namespace std;而typedef用來設為某個類型設置一個別名,如typedef unsigned long long uint64;不過,可能有些不知道,其實using也可以用來設置別名,在這種情況下,它與typedef所表述的意思沒有區別。使用using來設置一個別名方法如下:

using uint64 = unsigned long long;

C++標準上描述為:

A typedef-name can also be introduced by analias-declaration. The identifier following the using keyword becomes atypedef-name and the optional attribute-specifier-seq following the identifierappertains to that typedef-name. It has the same semantics as if it wereintroduced by the typedef specifier. In particular, it does not define a newtype and it shall not appear in the type-id

?因此在普通的類型名定義時,理論上講二者是沒有區別的,當然,我們知道,還有一種方式定義類型別名,效果也是一樣的,那就是#define。

using與typedef在C++11標準以后對于模板類型別名聲明有了一點區別。考慮到如下寫法:

template <typename T> typedef std::vector<T> v;//使用typedeftemplate <typename T> using v = std::vector<T>;//使用using

看起來好像是都可以的寫法,但是使用typedef時,編譯器會報錯error: template declaration of ‘typedef’

也就是說,C++編譯器不支持使用typedef關鍵詞為模板類設置別名,但是使用using的方式聲明一個關鍵詞卻是允許的,只是這個是C++11標準才有的,如果在編譯時不加上--std=c++11使用新的標準的話,編譯器一樣會報錯。

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的使用using与typedef来定义别名的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。