Names namespace inside a .C file

Q

qazmlp

One of the implementation file(.C) file has the following code

namespace Utils
{
// declaration & definition of some functions
}


Is this way having named namespace( inside a .C file instead of a .h)
recommended for any case ?
 
S

Shane Beasley

One of the implementation file(.C) file has the following code

namespace Utils
{
// declaration & definition of some functions
}

If a function is both declared *and* defined in the source file, you
should use an anonymous namespace instead:

namespace {
void f () { ... }
}

Namespaces are useful because they hide functions from the compiler
until you tell the compiler where to find them. If a function's
declaration is visible only in a single source file, then the function
should be made invisible to all other source files. Anonymous
namespaces effectively do just that.
Is this way having named namespace( inside a .C file instead of a .h)
recommended for any case ?

I can't think of a good reason to have a *named* namespace for
functions which, for all intents and purposes, do not exist outside
of a single source file.

- Shane
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top