Thread: namespace std - After searching the the boards

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    namespace std - After searching the the boards

    I've decided to switch to a ANSI-C++ standard compiler after using an old borland one for a while. Before i did that i was trying to figure out what the thing was with namespace std and i fully understand it, except one question still looms for me. Is this standard namespace a file, does it reside with the compiler, or what? And since all functions objects and variables are defined within this namespace, why do we still need library files (not header) to be linked? or do we?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    All of the standard functions are in the namespace std to avoid namespace collisons the standardfunctions are prototyped in the header files but they are actully defined in the lib's(well this is the way i understand it) so you still need the libs for the linker to actully know what the function does.
    Woop?

  3. #3
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    I still don't understand.

    The lib files and the standard namespace seem contridictory. On one hand i've read the lib files contain the definitions for all library functions / objects / variables, and on the other i've read that namespace std contain the definitions for all library functions / objects / variables. They seem related, but how?

    And the other part of my question:
    Is this standard namespace a file, does it reside with the compiler, or what?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Ok think of it like this you write your own cout function that has nothing do to with the standard namespace's cout. You put it in namespace mycout so in order for the compiler to know which cout you want to use you have to declare that you are using mycout::cout. As for where the namespace resides. I know for a fact its in the header files so im pretty sure it resides in the libs as well. If your still not getting it you can read this little tut on namespaces http://www.cplusplus.com/doc/tutorial/tut5-2.html
    Woop?

  5. #5
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    I'm still completely lost (well maybe not completely).

    I can create my own namespace like so:
    Code:
    namespace test
       {
        int a, b;
       }
    So doesn't the code for the standard namespace (std) have to be somewhere?

    And when you #include a header file, it's an instruction for the compiler to include it in the contents in your source code, which means all the objects and functions of that header file are in the scope of your program, so how can they be 'protected' by namespace std?

    And what is the difference between namespace std and library files when they both contain definitions for the objects / functiions?

    On the Side: Don't mean to sound rude prog-bman, but you're not really answring my questions, or i'm not being clear. Thank you for the link though
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Fine lemme explain myself better all of the classes you use for basic stuff here lemme post some code directly from a header file
    Code:
    namespace std 
    {
      // 27.4.5  Template class basic_ios
      /**
       *  @brief  Virtual base class for all stream classes.
       *
       *  Most of the member functions called dispatched on stream objects
       *  (e.g., @c std::cout.foo(bar);) are consolidated in this class.
      */
      template<typename _CharT, typename _Traits>
        class basic_ios : public ios_base
        {
        public:
          //@{
          /**
           *  These are standard types.  They permit a standardized way of
           *  referring to names of (or names dependant on) the template
           *  parameters, which are specific to the implementation.
          */
          typedef _CharT                                 char_type;
          typedef typename _Traits::int_type             int_type;
          typedef typename _Traits::pos_type             pos_type;
          typedef typename _Traits::off_type             off_type;
          typedef _Traits                                traits_type;
    see all of the classes you use are in the std namespace notice these are just declarations.
    >And when you #include a header file, it's an instruction for the compiler to include it in the contents in your source code, which means all the objects and functions of that header file are in the scope of your program, so how can they be 'protected' by namespace std?
    Try to use any standard function without the std:: and see what happens when you compile. You'll get an error just cause your including it doesn't mean you saying your using that namespace in your program.
    Im sorry im not clear maybe someone else could explain it better but this is the way I undertand it to be
    Im' guessing the libs which are precompiled look similar in their use of the namespace. Heres a kinda lame example of such things
    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    namespace loser
    {
      int subtract(int a,int b);
    }
    namespace loser
    {
      int subtract(int a,int b)//this would be in the lib 
      {
        return a+b;
      }
    }  
    int subtract(int a,int b);
    
    int main()
    {
      cout<<subtract(10,5);
      cout<<loser::subtract(10,5);
      return 0;  
    }
    int subtract(int a,int b)
    {
      return a-b;
    }
    Woop?

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    AFAIK, namespaces are declared within header files, etc., not the other way around. Likewise, namespaces can span multiple header files. Like this:
    Code:
    //Dog.h
    namespace one
    {
      string bark();
    }
     
    //Dog.cpp
    #include "Dog.h"
    string bark()
    {
      return "bow wow";
    }
     
    //Sound.h
    namespace two
    {
      string bark();
    }
     
    //Sound.cpp
    string bark()
    {
      return "wuffff";
    }
     
    //Train.h
    namespace two
    {
      string blowWhistle();
    }
     
    //Train.cpp
    string blowWhistle()
    {
      return "tooot toooooooot";
    }
     
    //driver program 
    #include <iostream>
    #include "Dog.h"
    #include "Sound.h"
    #include "Train.h"
     
    int main()
    {
      std::cout << one::bark() << std::endl;
      std::cout << two::bark() << std::endl;
      std::cout << two::blowWhistle() << std::endl;
    }
    I suspect if you look in a std lib files like iostream you will find syntax declaring that all functions, types, variables etc. within the file are declared in namespace std.

    Note: AFAIK there is no restriction to the the number of namespaces that can be used within a given file. However, for files having the standardized, precompiled functions found in namespace std, I believe they all use just namespace std, and no other namespaces.

  8. #8
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    Thanks prog-bman, that waas a better explination, it must have been the code
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  9. #9
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    Oh, and one last thing. the statement

    using std::cout;

    brings cout into the global scope for the duration of the program, but say you use it like this:

    std::cout << "blah";

    what exatcly happens here, since cout 's lifetime is only until the statment finshes executing?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  10. #10
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Make sure you are distinguishing between the name cout, and the actual instance of an ostream object that is called cout. When you type using std::cout;, you are bringing the name cout into the current scope. When you type std::cout << "blah";, you are simply qualifying the name by specifying the namespace that it is in.

    The lifetime of the cout instance is a completely separate concept, and has nothing to do with how you distinguish which name you are using.

  11. #11
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    When you type std::cout << "blah";, you are simply qualifying the name by specifying the namespace that it is in.
    But it's a one time thing right? What i mean is that since you arn't applying the 'using' keyword when you type this statement:

    std::cout << "blah";

    cout is not brought into the current scope, so what happens?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  12. #12
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    cout is not brought into the current scope, so what happens?
    It simply locates 'cout' in the 'std' namespace and runs the '<<' operator function with a parameter of "blah".
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thoughts about using namespace std?
    By dwks in forum C++ Programming
    Replies: 40
    Last Post: 09-25-2008, 05:14 PM
  2. std namespace
    By bigSteve in forum C++ Programming
    Replies: 8
    Last Post: 01-20-2004, 02:56 AM
  3. using namespace std; question.
    By fuh in forum C++ Programming
    Replies: 2
    Last Post: 12-30-2002, 04:39 PM
  4. using namespace std;
    By drdroid in forum C++ Programming
    Replies: 8
    Last Post: 09-15-2002, 08:56 AM
  5. using namespace std;
    By nikkimarie36 in forum C++ Programming
    Replies: 0
    Last Post: 08-03-2002, 03:47 PM