Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why should i use :std::: everytime? #10

Closed
mdsiaofficial opened this issue Nov 17, 2022 · 2 comments
Closed

why should i use :std::: everytime? #10

mdsiaofficial opened this issue Nov 17, 2022 · 2 comments

Comments

@mdsiaofficial
Copy link

some other codes/other websites, I found they don't use "std::". instead, some of them said that "using namespace std" after header filer is replacing this "std::".
is this a good practice to use the "std::" every time?

@Manoffsteel
Copy link

i think no

@dluffy121
Copy link

dluffy121 commented Apr 11, 2023

@mdsiaofficial
It's about good practice, for a simple project like this where the purpose is to just experiment its fine to use "using namespace ". But for larger projects usage of this is not recommended, as it may get confusing if the included headers files also have them.

Example:

// temp.cpp

#include <iostream>
using namespace std;

void something()
{
    cout << "something" << endl;
}
// main.cpp

#include "temp.cpp"

void cout()
{
    // to confuse
}

int main()
{
    std::cout << "doing something" << std::endl;  // will work fine as we explicitly mention the namepsace "std"
    cout << "will get confused now" << endl;  // compile error will get confused
    cout();  // compile error will get confused
    something();
    return 0;
}

Although you can avoid this by putting temp.cpp code into a namespace temp and in main cpp create a "using namepace " for it, but then the cycle may repeat for someone trying to include main.cpp

@rutura rutura closed this as completed Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants