Is there a way to qualify a namespace from only one library?

This is a discussion on Is there a way to qualify a namespace from only one library? within the C++ Programming forums, part of the General Programming Boards category; Let's say instead of using the using namespace std and have the name declared regardless of the header file, is ...

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Is there a way to qualify a namespace from only one library?

    Let's say instead of using the using namespace std and have the name declared regardless of the header file, is there a way to use the namespace std in only one header file, for instance the algorithm header file. Is there a way to do that.

  2. #2
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    I am not sure about a single library, but you can qualify each individual name you want
    Code:
    using std::cout;
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    well that I know about. I was thinking just names within the scope of a specific library.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    One possible way is to enclose the header in a namespace and have the using directive inside it.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Uh, no, that won't work, and might be forbidden. Consider:
    Code:
    #include <iterator>
    namespace mine {
    #include <algorithm>
    using namespace mine::std;
    }
    Now what happens if algorithm requires something from iterator? Well, it would first include the iterator header itself. However, due to include guards, that wouldn't have any effect. Then it would try to access an element of that header. Since it is already in the namespace std (it thinks), it would use unqualified access. Which means it tries to access e.g. std::iterator_traits with the unqualified iterator_traits from, say, mine::std::copy. Nu-uh, won't work.

    It might actually be explicitely forbidden by the standard, but I'm not sure about that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    I used the wrong wording. I meant inside the header file... surround all or part of the code by a namespace declaration and use the directive inside that namespace.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Oh, I see.

    I wouldn't do that either. VC++ 2005 has a bug with this code:
    Code:
    #include <iostream>
    
    namespace mine
    {
      using namespace std;
      // ...
    }
    
    // All symbols from std are visible here, too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I guess in the end it would just be easier the way it was meant to be. Just was curious though, thanks for the input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. standrad library
    By sarahr202 in forum C++ Programming
    Replies: 11
    Last Post: 05-18-2009, 09:50 PM
  2. Shared library design problem
    By p1r0 in forum C++ Programming
    Replies: 9
    Last Post: 03-23-2009, 01:36 PM
  3. static data structure in a library
    By melight in forum C Programming
    Replies: 6
    Last Post: 01-10-2009, 11:12 AM
  4. Property Set Library (PSL) - Announcement
    By vultur_gryphus in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 05-29-2008, 07:04 AM
  5. data validation & namespace
    By tiange in forum C++ Programming
    Replies: 4
    Last Post: 07-05-2005, 03:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21