Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

How do you use the std namespace!

I just started revising C++ for my interviews, my goal is to understand the crux of the language and build a competitive programming practice around it. I came across the namespace controversy which differs from every programmers' style. Some use the namespaces for competitive programming and others prefer the later with building apps in C++.

According to me, a good coding practice should be carried across irrespective of projects.

Would you go with writing
Code
using namespace std
which imports all the objects from the "Standard Template Library" or would rather prefer using it with the scope resolution style?
Code
std::*object*


It would be great if you can provide a justification/reason around this.
https://stackoverflow.com/questions/1452...d-practice
(Jan 29, 2020 08:35 AM)int main() Wrote:  https://stackoverflow.com/questions/1452...d-practice

Could have googled it as well.
Wanted to know what people on HF specifically C++ developers go with!
I typically don't use the global namespace directive. Rather, just individually declare the namespace along with each type.
Code
using namespace std;//bad

using std::cout; //not great but ok
using std::cin //not great but ok

std::cin << x ;//preferred
std::cout >> x >> std::endl; //preferred
(Jan 29, 2020 08:41 AM)Sugawara Wrote:  Could have googled it as well.
Wanted to know what people on HF specifically C++ developers go with!

they google it too https://www.geeksforgeeks.org/using-name...d-practice
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
std::cout > using namespace std;

I almost never use "using namespace" in any context.
Namespaces are used for a reason.
(Jan 29, 2020 09:29 AM)akali Wrote:  I typically don't use the global namespace directive. Rather, just individually declare the namespace along with each type.
Code
using namespace std;//bad

using std::cout; //not great but ok
using std::cin //not great but ok

std::cin << x ;//preferred
std::cout >> x >> std::endl; //preferred

This is my answer too. Explicit is better than implicit.
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
(Jan 29, 2020 09:29 AM)akali Wrote:  I typically don't use the global namespace directive. Rather, just individually declare the namespace along with each type.
Code
using namespace std;//bad

using std::cout; //not great but ok
using std::cin //not great but ok

std::cin << x ;//preferred
std::cout >> x >> std::endl; //preferred

Exactly this.
As a general rule, I do not declare using statements.

I explicitly state the identifier I am using. For example, std::cin or std::cout, for the simple reason that in larger development projects (millions of lines of code) I do not want to be debugging redefinitions or other strangeness.

Explicitly stating the namespace of the identifier, while might be few more keystrokes, trust me, its better having exercising those fingers, than losing your temper later in the project. Bitter experience.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by leetAug 16, 2022 11:36 AM 7 348 Aug 16, 2022 11:36 AM
Last Post: Sajuuk
[Python] by SigmaApr 22, 2021 04:37 AM 2 120 Apr 22, 2021 04:37 AM
Last Post: caw
[.NET] by AjummaJan 25, 2019 02:54 PM 2 203 Jan 25, 2019 02:54 PM
Last Post: Ajumma