久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么使用未命名的命名空間以及它們的好處是

Why are unnamed namespaces used and what are their benefits?(為什么使用未命名的命名空間以及它們的好處是什么?)
本文介紹了為什么使用未命名的命名空間以及它們的好處是什么?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我剛剛加入了一個(gè)新的 C++ 軟件項(xiàng)目,我正在嘗試了解設(shè)計(jì).該項(xiàng)目經(jīng)常使用未命名的命名空間.例如,類定義文件中可能會(huì)出現(xiàn)這樣的情況:

I just joined a new C++ software project and I'm trying to understand the design. The project makes frequent use of unnamed namespaces. For example, something like this may occur in a class definition file:

// newusertype.cc
namespace {
  const int SIZE_OF_ARRAY_X;
  const int SIZE_OF_ARRAY_Y;
  bool getState(userType*,otherUserType*);
}

newusertype::newusertype(...) {...

哪些設(shè)計(jì)考慮可能會(huì)導(dǎo)致使用未命名的命名空間?有什么優(yōu)點(diǎn)和缺點(diǎn)?

What are the design considerations that might cause one to use an unnamed namespace? What are the advantages and disadvantages?

推薦答案

Unnamed namespaces is a utility to make an identifier translation unit 本地.它們的行為就像您會(huì)為命名空間的每個(gè)翻譯單元選擇一個(gè)唯一名稱:

Unnamed namespaces are a utility to make an identifier translation unit local. They behave as if you would choose a unique name per translation unit for a namespace:

namespace unique { /* empty */ }
using namespace unique;
namespace unique { /* namespace body. stuff in here */ }

使用空主體的額外步驟很重要,因此您已經(jīng)可以在命名空間主體中引用在該命名空間中定義的諸如 ::name 之類的標(biāo)識(shí)符,因?yàn)?using 指令已經(jīng)發(fā)生.

The extra step using the empty body is important, so you can already refer within the namespace body to identifiers like ::name that are defined in that namespace, since the using directive already took place.

這意味著您可以擁有稱為(例如)help 的自由函數(shù),這些函數(shù)可以存在于多個(gè)翻譯單元中,并且它們不會(huì)在鏈接時(shí)發(fā)生沖突.其效果幾乎與使用 C 中使用的 static 關(guān)鍵字相同,您可以將其放入標(biāo)識(shí)符的聲明中.未命名的命名空間是一個(gè)更好的選擇,甚至可以將類型轉(zhuǎn)換單元設(shè)為本地.

This means you can have free functions called (for example) help that can exist in multiple translation units, and they won't clash at link time. The effect is almost identical to using the static keyword used in C which you can put in in the declaration of identifiers. Unnamed namespaces are a superior alternative, being able to even make a type translation unit local.

namespace { int a1; }
static int a2;

兩個(gè) a 都是本地翻譯單元,不會(huì)在鏈接時(shí)發(fā)生沖突.但不同的是匿名命名空間中的a1獲得了唯一的名字.

Both a's are translation unit local and won't clash at link time. But the difference is that the a1 in the anonymous namespace gets a unique name.

閱讀 comeau-computing 上的優(yōu)秀文章為什么使用未命名命名空間而不是靜態(tài)命名空間? (Archive.org 鏡像).

Read the excellent article at comeau-computing Why is an unnamed namespace used instead of static? (Archive.org mirror).

這篇關(guān)于為什么使用未命名的命名空間以及它們的好處是什么?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時(shí)分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運(yùn)算符的數(shù)學(xué)式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區(qū)別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達(dá)式中的變量聲明)
主站蜘蛛池模板: 一级毛片成人免费看a | aaaaaaa片毛片免费观看 | 欧美区日韩区 | 中文字幕在线观看视频网站 | 午夜免费在线 | 欧美1区2区| 成人国产精品久久久 | 精品一区av | 情侣酒店偷拍一区二区在线播放 | 久久人爽| 久久精选| 91精品久久久久久久久中文字幕 | 亚洲天堂av一区 | 天天草天天爱 | 欧美精品黄 | 久久久久久久久久久高潮一区二区 | 天天影视亚洲综合网 | 日日日日操| 激情影院久久 | 粉嫩av久久一区二区三区 | 日韩毛片播放 | 欧美区在线 | 国产日韩一区二区三免费高清 | 在线观看av网站永久 | 亚洲国产aⅴ精品 | 日韩成人影院在线观看 | 欧美人妖网站 | 91视频在线观看 | 夜夜夜夜草 | 久久1区| 成人av电影免费在线观看 | 特级黄一级播放 | 国产高清视频一区二区 | 日韩精品一二三区 | 日日综合| 欧美久久久久久久久中文字幕 | 伊人导航 | 性色av一区 | 日韩不卡在线观看 | 亚洲欧美一区二区三区视频 | 91久久|