C++ programmers! How do you use unnamed 'namespace's ?

R

Razmig K

Dear mates,
This is a small survey for C++ programmers to speak their mind about
the uses and/or potential advantages and disadvantages resulting from
the use of the aforementioned C++ feature in ralistic code in various
programming domains.
Thank you.

//rk
 
I

Ivan Vecerina

Hi Razmig,

| This is a small survey for C++ programmers to speak their mind about
| the uses and/or potential advantages and disadvantages resulting from
| the use of the aforementioned C++ feature in ralistic code in various
| programming domains.

I *always* use an anonymous namespace around small utility classes
(e.g. predicates and functors for STL algorithm calls) defined in
an implementation (.cpp) file. This is the easiest and the only safe
way to avoid name clashes in C++.

For isolated internal functions and global variables (if ever...),
I may still use 'static' instead of an anonymous namespace.
In spite of it being deprecated -- old habits die hard...
(and using 'static' helps portability to older & C compilers).

Realistic code: absolutely
Programming domain: from embedded/RT SW to Mloc OO/GUI applications.


Cheers,
Ivan
 
J

jeffc

Razmig K said:
Dear mates,
This is a small survey for C++ programmers to speak their mind about
the uses and/or potential advantages and disadvantages resulting from
the use of the aforementioned C++ feature in ralistic code in various
programming domains.

They are useful either in large programs or when using third party libraries
(where you're using someone else's code, but can't change it) to avoid name
clashes.
 
J

John L Fjellstad

Razmig said:
This is a small survey for C++ programmers to speak their mind about
the uses and/or potential advantages and disadvantages resulting from
the use of the aforementioned C++ feature in ralistic code in various
programming domains.

I use it when I want to introduce filescope. For instance, I'm using an
algorithm like for_each(), and I want to have functor to pass to it, then I
put the functor in the anonymous namespace in the .cpp file. Kinda a
replacement for static.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top