Why do you use namespace in C++?

  • Context: C/C++ 
  • Thread starter Thread starter Avichal
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the purpose and necessity of namespaces in C++, particularly in comparison to C and other programming languages. Participants explore the implications of namespaces for avoiding naming collisions in libraries and the historical context of language design.

Discussion Character

  • Exploratory
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants propose that namespaces are primarily used to avoid naming collisions between libraries, a feature not present in C.
  • Others note that while C++ and C# have namespaces, languages like Java and Python use packages and modules, respectively, to achieve similar goals.
  • One participant expresses uncertainty about the necessity of namespaces, questioning whether they truly help since functions can always have different names.
  • Another participant argues that namespaces significantly increase the set of possible collision-free names, making them a valuable convenience, even if not strictly necessary.
  • A later reply suggests that the absence of namespaces in C may be due to the language's age and the simpler programming needs of the time when it was created.

Areas of Agreement / Disagreement

Participants generally agree that namespaces help avoid naming collisions, but there is some uncertainty regarding their necessity and the historical reasons for their absence in C.

Contextual Notes

Limitations in understanding namespaces and their implications are expressed, particularly regarding the evolution of programming practices since C's inception.

Who May Find This Useful

Readers interested in programming language design, library management, and the evolution of coding practices may find this discussion relevant.

Avichal
Messages
294
Reaction score
0
As far as I could understand namespaces are used to avoid collision between libraries? C doesn't have this feature, then why was this feature necessary in c++? And do other languages have this feature too?
 
Technology news on Phys.org
C++ and C# have namespaces, java has packages, python has modules, perl has modules. C doesn't have anything like this. You either emulate the concept with big long names or suffer the consequences.
 
I don't think I understand namespaces that well. So here is my understanding: - In C two libraries cannot have same names of functions as if we include both we'll have a conflict.
So to avoid this conflict we have namespaces. So when we use std namespace we only refer to libraries that are under std.
Am I right?

But does namespace really help? We can always have different names of functions right?
 
Avichal said:
But does namespace really help? We can always have different names of functions right?

What happens then if you end up using two libraries that end up having the same function name?

The set of possible collision-free names is much larger with namespaces than it is without. Namespaces are a nice feature. Strictly speaking, they're not absolutely necessary, but they are a huge convenience.
 
jhae2.718 said:
What happens then if you end up using two libraries that end up having the same function name?

The set of possible collision-free names is much larger with namespaces than it is without. Namespaces are a nice feature. Strictly speaking, they're not absolutely necessary, but they are a huge convenience.

Yeah okay I guess they might be useful. Since I have not made any programs that require use of many libraries I might not realize it. Anyways, why didn't C include ths feature then?
 
C is relatively ancient as a computer language. When it was invented in the 1970s, people didn't write programs that are nearly as large and complex as they can be nowadays.
 
Got it, thank you guys!
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 40 ·
2
Replies
40
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 70 ·
3
Replies
70
Views
5K
  • · Replies 118 ·
4
Replies
118
Views
10K
Replies
86
Views
4K
  • · Replies 15 ·
Replies
15
Views
8K
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K