We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

ibanja
ibanja asked
on
Medium Priority
509 Views
Last Modified: 2010-04-01
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
Comment
Watch Question

AxterSenior Software Engineer

Commented:
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!
CERTIFIED EXPERT
Top Expert 2012
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
"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;

Author

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.
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
Yes, but it is common practice to put all of you "using" statements together at the top of the relevant file.
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
You can, but you are risking conflicts if these namespaces contain types that are named identically.

Author

Commented:
Thanks,

I think I've got it.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.