What's the best way to not keep typing in long namespaces before functions and variables?

What is the best way to avoid the long namespaces such as lucene::index::IndexWriter?

Is it correct to just use the following:
using namespace lucene::index;
IndexWriter writer = new IndexWriter(indexDir, analyzer, true);

then change to another namespace:
using namespace lucene::analysis;
more code ...

using namespace std;
cout << "Hi";

Thanks,
ibanja
ibanjaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AxterCommented:
Hi ibanja,
> Is it correct to just use the following:
> using namespace lucene::index;

Only in your *.cpp source file.
You should avoid doing this in the headers, and instead use fully qualified names.

David Maisonave (Axter)
Cheers!
0
jkrCommented:
In order to avoid namespace conflichts, you could narrow that down to

using lucene::index::IndexWriter;
IndexWriter writer = new IndexWriter(indexDir, analyzer, true);

and not introduce the whole namespace at all.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Carl TawnSystems and Integration DeveloperCommented:
"then change to another namespace:" <-- You can use multiple "using" statements in the same file.

i.e.

   using namespace lucene::index;
   using namespace lucene::analysis;
   using namespace std;

   IndexWriter writer = new IndexWriter(indexDir, analyzer, true);

Although purests would frown on just using "namespace std;" and would suggest that you should explictly declare which members of the namespace you are using:

i.e.

    using std::cout;
    using std::cin;
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

ibanjaAuthor Commented:
Thanks,

So if I understand correctly - multiple namespaces can be in use and:

using namespace lucene::index;
using namespace lucene::analysis;
using namespace std;
code ...
code ...
code ...

is the same as:

using namespace lucene::index;
code ...
using namespace lucene::analysis;
code ...
using namespace std;
code ...

since setting one name space doesn't nullify another namespace.
0
Carl TawnSystems and Integration DeveloperCommented:
Yes, but it is common practice to put all of you "using" statements together at the top of the relevant file.
0
jkrCommented:
You can, but you are risking conflicts if these namespaces contain types that are named identically.
0
ibanjaAuthor Commented:
Thanks,

I think I've got it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.