Skip to content

Commit

Permalink
wiki: avoid using namespace std; in C++ (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oreoxmt authored Dec 13, 2023
1 parent 0d081d0 commit 0523969
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
31 changes: 31 additions & 0 deletions website/docs/cpp/cpp-wiki.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: "C++"
description: "A collection of useful C++ usage and tips."
---

## Avoid `using namespace std;`

It is common to encounter `using namespace std;` in various C++ sample codes, as it negates the need to prefix `std::` before each standard library object. However, it is not recommended to use it in your code. The reason is that `using namespace std;` might cause name conflicts.

Consider a scenario where you have a function or variable named `max`. If you declare `using namespace std;`, then you will get an error when you try to use `max` because it conflicts with `std::max`.

```cpp
#include <iostream>
using namespace std;

int max = 0;
int main() {
cout << max << endl; // error: reference to 'max' is ambiguous
}
```

To avoid the preceding error, use the following code instead:

```cpp
#include <iostream>

int max = 0;
int main() {
std::cout << max << std::endl; // 0
}
```
10 changes: 10 additions & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ const sidebars = {
],
},
'git-wiki',
{
type: 'category',
label: 'C++',
link: {
type: 'generated-index',
},
items: [
'cpp/cpp-wiki'
],
},
{
type: 'category',
label: 'Python',
Expand Down

0 comments on commit 0523969

Please sign in to comment.