namespaces

T

The Code Master

I have been having a problem with namespaces. This may be a Win-MFC issue,
but am I correct that the following should have file level scope?

using namespace MyNamespace;

Is there a way for me to declare this to have program scope?
What I would like, is to have a namespace with commonly used functions and
classes, available with program scope.

For you with experience on many different types of compilers, is this
implemented differently on different compilers? In other words, could that
be the issue?

John
(e-mail address removed)
 
J

John Carson

The Code Master said:
I have been having a problem with namespaces. This may be a Win-MFC
issue, but am I correct that the following should have file level
scope?

using namespace MyNamespace;

Assuming it is declared at global scope in the file, yes.
Is there a way for me to declare this to have program scope?

Only by putting it in a header that is #included in every file.
What I would like, is to have a namespace with commonly used
functions and classes, available with program scope.

For you with experience on many different types of compilers, is this
implemented differently on different compilers? In other words,
could that be the issue?

It is a language issue, not a compiler issue (except that compilers may, of
course, offer non-standard extensions that permit all sorts of things not
specified in the standard).
 
G

Greg

The said:
I have been having a problem with namespaces. This may be a Win-MFC issue,
but am I correct that the following should have file level scope?

using namespace MyNamespace;

Is there a way for me to declare this to have program scope?
What I would like, is to have a namespace with commonly used functions and
classes, available with program scope.

Why put these functions into a namespace if the goal is to make them
available in the global namespace? The goal here would appear to be at
odds with the steps being taken.
For you with experience on many different types of compilers, is this
implemented differently on different compilers? In other words, could that
be the issue?

The issue would appear to be declaring the functions and classes in the
wrong namespace. Generally, the implementor places the interface to the
implementation in a namespace. Clients of the implementation can decide
to bring those names into their global namespace when and where they
wish. If the implementor decides that the interface should be in the
global namespace of every client, then he or she should simply declare
the interface in the global namespace to start with, rather than
declare it somewhere else and then have to figure out how to move it.

Greg
 
A

AnonMail2005

Is there a way for me to declare this to have program scope?
Only by putting it in a header that is #included in every file.
Except this is not good practice. Using declarations and using
directives should not be used in header files. Why?

The short answer is because the effect of these statements can
depend on the the order of includes!!

Item #59 of Sutter and Alexandrescu's C++ Coding Standards gives
all the gory details and some very good examples to explain how
this can occur.
 
J

John Carson

Except this is not good practice. Using declarations and using
directives should not be used in header files. Why?

The short answer is because the effect of these statements can
depend on the the order of includes!!

Item #59 of Sutter and Alexandrescu's C++ Coding Standards gives
all the gory details and some very good examples to explain how
this can occur.

Quite so, though if it was a header file that was to be included in every
file, you would normally standardize on always including it first.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top