2010-02-07 301 views

回答

42

由於C++標準已被接受,幾乎所有的標準庫的是std命名空間的內部。因此,如果您不想通過std::限定所有標準庫調用,則需要添加using指令。

然而,

using namespace std; 

被認爲是不好的做法,因爲你幾乎導入整個標準的命名空間,從而打開了很多的名稱衝突的可能性。這是更好地導入只有你實際使用的東西在你的代碼,就像

using std::string; 
+14

+1因爲是唯一一個提到它而皺眉。 – GManNickG 2010-02-07 20:17:40

23

沒有任何東西,這是一個縮寫,以避免前綴在該命名空間中std ::一切

+11

而且也被認爲是不好的做法。 – GManNickG 2010-02-07 20:18:49

+2

其糟糕的做法,如果你這樣做全局命名空間:D – 2010-02-07 20:22:02

+7

@GMan,@Hassan:在實現文件中使用它,並在頭文件中是危險的。爲什麼每個人都會說「不好的做法」?我不想在代碼中使用'std ::',因爲我不想使用'namespace some_tools'隱式地導入任何名稱空間;' – Potatoswatter 2010-02-07 21:54:31

5

指會員在std命名空間,而不需要參考std::member明確的能力。例如:

#include <iostream> 
using namespace std; 

... 
cout << "Hi" << endl; 

#include <iostream> 

... 
std::cout << "Hi" << std::endl; 
3

首先,這不是必須用C - C沒有命名空間。在C++中,包含大部分標準庫的std名稱空間中的任何內容。如果你不這樣做,你必須明確地訪問該命名空間的成員,像這樣:

std::cout << "I am accessing stdout" << std::endl; 
0

所有的C++標準庫文件申報其所有實體的std命名空間內。
e.g:要使用在iostream的定義

替代cin,cout

using std::cout; 
using std::endl; 
cout << "Hello" << endl;
std::cout << "Hello" << std::endl;

0

它只要您使用的是一個命名空間內聲明的東西使用。 C++標準庫在名稱空間std中聲明。因此,你必須做

using namespace std; 

,除非你想另一個命名空間中調用函數時指定的命名空間,就像這樣:

std::cout << "cout is declared within the namespace std"; 

您可以在http://www.cplusplus.com/doc/tutorial/namespaces/閱讀更多關於它。

3

首先,因爲C根本不支持命名空間,所以在C中不需要指令using

using指令實際上從未需要用C++因爲任何在命名空間中的項目可以通過std::前綴,而不是他們直接訪問。因此,舉例來說:

using namespace std; 
string myString; 

等同於:

std::string myString; 

無論您選擇使用它是偏好的問題,但暴露了整個std命名空間來保存幾個按鍵一般被認爲是不良形式其中僅在命名空間中暴露特定項目的另一種方法如下:

using std::string; 
string myString; 

這可以讓你只暴露在std命名空間中,你特別需要的項目,沒有你沒有無意中暴露出一些風險打算。

0

你永遠不必聲明using namespace std;使用它是不好的做法,你應該使用std ::如果你不想打字的std ::總是你可以做在某些情況下是這樣的:

using std::cout; 

使用的std ::你也可以告訴程序的哪一部分使用標準庫,哪一部分不使用。更重要的是,可能會與其他包含的功能發生衝突。

RGDS 萊恩

+2

在頭文件的全局名稱空間中這只是一個不好的做法。在實現文件中,這通常是一個好主意。保存打字無關緊要 - 您的編輯應該爲您打字。這很好,因爲它使代碼比任何地方都具有'std ::'更具可讀性,並且比每個文件頂部有三十行'使用std :: whatever;'更易於維護。 – Porculus 2010-02-07 22:47:30

5

你絕對不應該說:

using namespace std; 
在C++頭

,因爲拍使用名稱空間(這樣做會構成「命名空間污染」)的整點。有關這個主題的一些有用的資源如下:上Standard convention for using 「std」

1)計算器線程)由香草薩特的文章Migrating to Namespaces

3)FAQ 27.5從馬歇爾克萊因的C++ FAQ精簡版。

1

命名空間是一種包裝代碼的方式,以避免混淆和名稱衝突。例如:

文件common1.h:

namespace intutils 
{ 
    int addNumbers(int a, int b) 
    { 
     return a + b; 
    } 
} 

使用文件:

#include "common1.h"  
int main() 
{ 
    int five = 0; 
    five = addNumbers(2, 3); // Will fail to compile since the function is in a different namespace. 
    five = intutils::addNumbers(2, 3); // Will compile since you have made explicit which namespace the function is contained within. 

    using namespace intutils; 
    five = addNumbers(2, 3); // Will compile because the previous line tells the compiler that if in doubt it should check the "intutils" namespace. 
} 

所以,當你寫using namespace std所有你正在做的是告訴編譯器,如果有疑問,應該看在函數的std名稱空間等中,它無法找到定義。這在示例(和生產)代碼中通常使用,因爲它使得鍵入常用函數等,如cout比必須完全限定每一個作爲std::cout更快。

7

從技術上講,您可能需要使用,使用(對於整個名稱空間或單個名稱)才能使用參數相關查找。

考慮使用swap()的以下兩個函數。

#include <iostream> 
#include <algorithm> 

namespace zzz 
{ 
    struct X {}; 


void swap(zzz::X&, zzz::X&) 
{ 
    std::cout << "Swapping X\n"; 
} 
} 

template <class T> 
void dumb_swap(T& a, T& b) 
{ 
    std::cout << "dumb_swap\n"; 
    std::swap(a, b); 
} 

template <class T> 
void smart_swap(T& a, T& b) 
{ 
    std::cout << "smart_swap\n"; 
    using std::swap; 
    swap(a, b); 
} 

int main() 
{ 
    zzz::X a, b; 
    dumb_swap(a, b); 
    smart_swap(a, b); 

    int i, j; 
    dumb_swap(i, j); 
    smart_swap(i, j); 
} 

dumb_swap總是調用std::swap - 即使我們寧願更喜歡使用zzz::swapzzz::X對象。

smart_swap使得std::swap作爲後備選擇可見的(例如在與所謂的整數),但因爲它不完全限定名稱,zzz::swap將通過ADL爲zzz::X使用。


主觀上,什麼迫使我使用using namespace std;被編寫使用各種標準的函數對象的代碼等

//copy numbers larger than 1 from stdin to stdout 
remove_copy_if(
    std::istream_iterator<int>(std::cin), std::istream_iterator<int>(), 
    std::ostream_iterator<int>(std::cout, "\n"), 
    std::bind2nd(std::less_equal<int>(), 0) 
); 

IMO,在這樣的代碼std::只是使線路噪聲。

如果在實現文件中使用它(但它甚至可以限制在函數作用域中,如交換示例中),在這種情況下,我不會發現這種可憎的犯罪。

絕對不要把using語句放在頭文件中。原因是這會污染其他標題的名稱空間,可能會在違規之後包含其他標題,這可能會導致其他標題中可能不受您控制的錯誤。 (這也增加了意外因素:人,其中包括該文件可能無法期待各種名目的是可見的。)

0

沒有需要你做的 - 除非你是C++標準庫的實現者,你想在「新」和「舊」風格中聲明頭文件時避免代碼重複:

// cstdio 
namespace std 
{ 
    // ... 
    int printf(const char* ...); 
    // ... 
} 

// stdio.h 
#include <cstdio> 
using namespace std; 

嘛,當然例子是人爲(你同樣可以使用純<stdio.h>,並把它所有性病中<cstdio>),但Bjarne Stroustrup顯示了他的這個例子。

相關問題