You are on page 1of 3

3/10/13

Using namespaces properly - CodeProject


9,719,434 members (25,945 online) manish pratihasta 315 Sign out

home

articles

quick answers

discussions

features

community

help
Next

Search for articles, questions, tips

Articles Languages C / C++ Language Beginners

Article Browse Code Stats Revisions Alternatives Comments & Discussions (15)

Using namespaces properly


By Dejan Jelovic, 31 Jan 2001
3.00 (40 votes)

About Article
This article does not teach you the syntax of namespaces. Rather, it shows you how to use them properly. Type Licence First Posted Views
Collapse | Copy Code

Introduction
Namespaces are a very powerful C++ language feature. This article does not teach you the syntax of namespaces. Rather, it shows you how to use them properly. Namespaces simply wrap all enclosed names with another name. For example:

Article

31 Jan 2001 106,012 47 times

Add your own alternative version

Bookmarked VC6 Win2K

n a m e s p a c en e t{ c l a s sS o c k e t{ . . . } ; } . . . n e t : : S o c k e ts o c k e t ;

Visual-Studio Dev , +

By doing that, they make sure that if two libraries both implement the Socket class, if they name their namespaces differently your program can use both without a conflict. But this brings up another question: If two independent companies both decide to write network libraries, what are the chances that they are going to implement a class named Socket? My guess is somewhere around 100 percent. We also like it when namespace names are easy to type, which means that they should be 2-4 characters long. With that in mind, what are the chances that both companies are going to name their namespace net ? 5 percent? 10 percent? Whatever it is, it shows that namespaces do not solve the problem, they only make it less severe.

Top News
MySQL: Do Not Pass This Way Again
Get the Insider News free each morning.

Related Articles
Web Graphics On The Fly in ASP.NET Understanding Namespaces in C# 2.0 Converting Images to and from Base64Format Googles gtest not handling friend tests correctly Three-tier .NET Application Utilizing Three ORM Technologies WMI Namespace Security Using EventTrigger in XAML for MVVM No Code Behind How to Communicate Between Two Local Silverlight Applications? C# 2.0 Aliases ViewState Compression Refly, makes the CodeDom'er life easier Sandcastle Help File Builder Bringing AOP to MEF AGE: write your custom Graphic library Understanding Embedded Resources in Visual Studio .NET Parsing XML using a C++ wrapper for SAX2

An Industrial Strength Solution


The solution to this problem is to use long, unique namespace names, and then bring the namespaces into a program by using short aliases. So a company writing the network library should write something like:
Collapse | Copy Code

n a m e s p a c en e t _ 3 3 8 4 3 8 9 4{ c l a s sS o c k e t{ . . . } ; }

where the number after n e t _is going to be generated using a random number generator. Say this code is placed in a header file called <netlib> Then the library is sold to a client, who decides to use it on a project. The client then writes his own project-local header file named <mynetlib>, with the following content:
Collapse | Copy Code

# i n c l u d e< n e t l i b > n a m e s p a c en e t=n e t _ 3 3 8 4 3 8 9 4 ;

He has just created a project-local alias for the namespace of his library vendor. If the namespace name net had already been taken by another library, the user can choose another name: net2, sock, or something else. There will be no name conflicts.

Lowering Barriers
www.codeproject.com/Articles/949/Using-namespaces-properly

1/3

3/10/13

Using namespaces properly - CodeProject


A smart thing to do with your library is to make it easy for people to start using it. In an ideal world, they should be able to double-click on an installation file and the library would be immediately available inside their development environment. Next thing they are typing is #include <yourlib> and they are using it to do something useful. However, if the user has to make his own header for every header in your library, then he has to suffer a little bit in order to use it. Not every user will be willing to do so. The solution to this problem is to provide reasonable defaults, but to let the users cop out of them if they are not suitable. The way to do this is with preprocessor directives in your header file:
Collapse | Copy Code

Practical Guide to STL C# and XML Source Code Documentation A Guide to Cleaner XAML with Custom Namespaces and Prefixes (WPF/Silverlight) Add namespaces for Razor pages

n a m e s p a c en e t _ 3 3 8 4 3 8 9 4{ c l a s sS o c k e t{ . . . } ; } # i f n d e fN O _ N E T _ 3 3 8 4 3 8 9 4 _ A L I A S n a m e s p a c en e t=n e t _ 3 3 8 4 3 8 9 4 ; # e n d i f

This way, we provide a reasonable default for the namespace name. If that name is already taken, then the user can define the macro N O _ N E T _ 3 3 8 4 3 8 9 4 _ A L I A Sand no alias will be defined.

Current Compilers
Error messages are already a nightmare with templates. With long namespace names, we make them even worse. Unfortunately, none of the compilers I use are smart enough to display an error with the shortest available namespace alias at the point of error. So even though you may be using the alias net, if you make an error using the Socket class the error will mention n e t _ 3 3 8 4 3 8 9 4 : : S o c k e t . Not very readable. So I use a little trick. It works only for headers that contain only inline functions (as it affects the actual names used by the linker), but I have plenty of those. If the macro N O _ N E T _ 3 3 8 4 3 8 9 4 _ A L I A Sis not defined, I use the short name as the namespace name, and the long name as the alias:
Collapse | Copy Code

# i f d e fN O _ N E T _ 3 3 8 4 3 8 9 4 _ A L I A S n a m e s p a c en e t _ 3 3 8 4 3 8 9 4{ # e l s e n a m e s p a c en e t{ # e n d i f c l a s sS o c k e t{ . . . } ; } # i f n d e fN O _ N E T _ 3 3 8 4 3 8 9 4 _ A L I A S n a m e s p a c en e t _ 3 3 8 4 3 8 9 4=n e t ; # e n d i f

And the error messages become bearable again.

For more good stuff about C++, including a way to write code without memory errors, visit http://www.jelovic.com.

License
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below. A list of licenses authors might use can be found here

About the Author

Dejan Jelovic
Yugoslavia Member

No Biography provided

Article Top

Tw eet

Rate this:

Poor

Excellent

Vote

www.codeproject.com/Articles/949/Using-namespaces-properly

2/3

3/10/13

Using namespaces properly - CodeProject

Comments and Discussions


Add a Comment or Question
Profile popups Spacing Relaxed Search this forum Noise Medium Layout Normal Per page 25 Update First Prev Next Go

NO!! Don't do this! Just use nested namespaces. Re: NO!! Don't do this! Just use nested namespaces. Re: NO!! Don't do this! Just use nested namespaces. Great article for newbies - idea for extension... A better solution to your problem Re: A better solution to your problem HELP ME PLS!!!!!!!!!!!!!!! Re: HELP ME PLS!!!!!!!!!!!!!!! Re: HELP ME PLS!!!!!!!!!!!!!!! HELP ME PLS!!!!!!!!!!!!!!! Re: HELP ME PLS!!!!!!!!!!!!!!! An alternative to a random number

Don Clugston AndreRyan

29 Mar '04 - 18:50 29 Feb '08 - 23:03

Member 3680785

27 Jan '09 - 17:08

Paul Evans

12 Dec '02 - 16:35

Jakesf22
Dejan Jelovic

17 May '01 - 20:05 18 May '01 - 8:21 7 May '01 - 12:01 7 May '01 - 12:45 8 Jul '12 - 19:03 7 May '01 - 12:01 1 Jun '01 - 15:47 31 Jan '01 - 14:53

alharthi Anonymous
ekolis

alharthi AfterEleven
Jason De Arte

Re: An alternative to a random number -- not really Suggestion: URNs as namespace identifiers Re: Suggestion: URNs as namespace identifiers
Last Visit: 10 Mar '13 - 14:08 General News Last Update: 10 Mar '13 - 9:09 Question Bug

Dejan Jelovic

31 Jan '01 - 16:37

Jonathan Gilligan Dejan Jelovic

31 Jan '01 - 19:30 18 May '01 - 8:31

Refresh Answer Joke Rant Admin

Suggestion

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Permalink | Advertise | Privacy | Mobile Web01 | 2.6.130305.1 | Last Updated 31 Jan 2001 Layout: fixed | fluid Article Copyright 2001 by Dejan Jelovic Everything else Copyright CodeProject, 1999-2013 Terms of Use

www.codeproject.com/Articles/949/Using-namespaces-properly

3/3

You might also like