Thread: Difference between preprocessor and namespace

  1. #1
    Me
    Join Date
    Jul 2006
    Posts
    71

    Difference between preprocessor and namespace

    I've noticed that according to the tutorial, a preprocessor and a namespace are the same thing in a basic sense. But what exactly is each one of them. What are they used for, how are they different, and anything else you think would be helpful?

    Thanks,
    Relyt_123

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    A namespace is like a place to put functions and classes, where their names won't conflict with functions and classes of the same name in different namespaces.
    Code:
    namespace one {
        class c {};
    }
    
    namespace two {
        class c {};
    }
    
    one::c x;
    two::c y;
    The preprocessor is a program that parses your source files before they are compiled. It handles lines that start with '#', such as #include and #define.

    #include copies the entire contents of the included files into your source file. Often this is a header file containing function prototypes etc.

    #define searches for one string and replaces it with another. That is,
    Code:
    #define color colour
    will replace all occurences of "color" in your program with "colour" (outside of strings).

    #defines can also be macros, like this:
    Code:
    #define square(x) ((x) * (x))
    
    square(3)
    // turns into
    ((3) * (3))
    The extra parenthesis are in case something like "3+2" was passed as an argument. Without them it would turn into (3+2 * 3+2), which is something different. It's not perfect, though; passing something like "c++" would still mess it up: (c++ * c++). For this reason, inline functions are usually used instead of macros in C++.
    Code:
    inline int square(int x) { return x * x; }
    They're just like regular functions except the compiler will make them "inline" if it can, eliminating the call and return assembly instructions. This is efficient for small functions but a bad idea for large ones. (The compiler can ignore this request if it likes and make the function a regular one.)

    Functions declared inside a class are automatically inline.
    Code:
    class x {
        void inline() {}
        void notinline() {}
    }
    
    void x::notinline() {}
    . . . okay I'll stop my rant before I go too far off topic.
    Last edited by dwks; 01-10-2007 at 03:15 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I think you meant to say "defined inside a class" and
    Code:
    class x {
        void inline() {}
        void notinline();
    }
    
    void x::notinline() {}

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm not sure I see any similarities between namespaces and the preprocessor. They are completely different things. Could you elaborate on why you think they are the same?

    The only thing I can think of is the need to #include a header file and the need to specify the namespace that a name belongs to (e.g. you must #include <string> to use the string class and add std:: or a using directive for the std namespace to use the string class). On the surface I guess I can see that similarity, but they really are pretty different.

    First, the preprocessor does much more than #include, and namespaces are much more than just adding a using directive. But even #includes and using directives are different.

    A #include brings in the declarations of classes, objects, and functions from a header so that you can use them in your file. That way when the compiler compiles your code, it knows what the things you are using look like. For example, you need to #include <string> to get the code for the string class so the compiler can understand how to make your code work with the string class (what member functions are available, how much memory a string variable takes, etc).

    On the other hand a namespace just groups names together so that you don't have naming conflicts. Normally, you would write std::string so the compiler knows that string refers to the name of the class in the std namespace. It doesn't help the compiler know what string is, just that you want the thing with the name string from that namespace.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by robatino
    I think you meant to say "defined inside a class" and
    Code:
    class x {
        void inline() {}
        void notinline();
    }
    
    void x::notinline() {}
    Yes I did -- oops.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the difference in two times
    By muthus in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 06:36 PM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. Replies: 6
    Last Post: 08-26-2006, 11:09 PM
  5. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 05:20 AM