locked
Why i need to add"using namespace std" in order to write #include<iostream> RRS feed

  • Question

  • I know it's kind of basic but really appreciate your help if any
    • Moved by CoolDadTx Monday, January 12, 2015 3:51 PM C++ related
    Monday, January 12, 2015 2:57 PM

Answers

  • Please stop trying to make some sort of association between #include and using namespace. They are 2 completely different concepts for 2 completely different problems.  You might as well say that if you declare a variable called A that you don't need to use an if statement anymore. 

    #include

    • Handled by preprocessor, never seen by the compiler
    • Merges file content together before sending source to compiler
    • Completely optional (although almost always used)

    using namespace

    • Handled by compiler
    • Brings symbols into scope
    • Completely optional (not used in older code bases

    #include is a holdover from C (which didn't have namespaces) and is a consequence of how compilation was done back in the days C was written.  The only thing it does is take the contents of one file and puts them into another. That's it.

    It allows us to break up a large program into separate files for easier maintenance.  You are sort of spoiled with the modern IDEs so it probably isn't clear but the compiler only compiles a single .cpp file at a time. Every .cpp file compiles in isolation. Symbols defined in 1 .cpp file are not visible in another .cpp file as far as the compiler is concerned. 

    //File1.cpp
    void Foo () { }

    The problem then becomes how do you use a function (i.e. Foo) defined in one .cpp file (i.e. File1.cpp) in another .cpp file(I.e. File2.cpp).

    //File2.cpp
    void Bar ()
    {
       Foo(); //Compiler error, no such symbol defined in File2.cpp
    }

    The C++ language requires that a symbol be declared before it is used. As such the compiler requires that Foo be declared before File2.cpp uses it. But since the function is declared in File1.cpp the compiler knows nothing about it.  You could add the declaration to File2.cpp directly but this introduces maintenance issues and can result in a lot of repeated code if you're reusing a lot of functionality.

    //File2.cpp
    //Possible, but not recommended
    void Foo ();

    void Bar ()
    {
       Foo();
    }

    To solve this you tend to put the declarations into a header file. 

    //File1.h
    void Foo ();

    Then both .cpp files can reference it.

    //File1.cpp
    #include "File1.h"

    //File2.cpp
    #include "File2.h"

    void Bar ()
    {
       Foo();
    }

    If you need to change or add a symbol you do it in one place and both files see the changes. Now when the compiler runs it finds the definition of Foo and it can continue.  It is literally no different than if you had hand written the declarations in each of your .cpp files yourself.

    //What the compiler sees - File2.cpp
    void Foo ();

    void Bar ()
    {
       Foo();
    }

    Of course header files can have more than just declarations but the concept is the same. In general a header file name matches the corresponding .cpp file for convenience but the actual name is completely irrelevant other than in the #include line.

    Namespaces solve a completely different problem.  By default declarations are in the global scope.  As programs get larger you start running into name collisions.  The language requires that a symbol be unique in its scope (ignoring function overloading). So as you break up your program into separate files you start to run the risk of collisions. For example you might use a library that has a Log function and you might use another library that manages woodcutters with its own Log function.

    //SomeLibrary.h
    void Log ();

    //MyLibrary.h
    bool Log;

    If both these declarations make it into the same .cpp file (such as via #include) then the compiler will fail as it doesn't know which one to use. 

    //App.cpp
    #include "SomeLibrary.h"
    #include "MyLibrary.h"

    void Bar ()
    {
       Log();  //Compiler error as we don't know which one to use
    }

    To help alleviate that C++ added the concept of namespaces.  A namespace is a simple (hierarchial) grouping of related symbols. It allows you to move symbols out of the global scope and into a named scope. This reduces the likelihood of collisions and organizes related functionality together so it is easier to find.  For example a library might put all its public symbols into a namespace that matches the name of the library (not the file). 

    //SomeLibrary.h
    namespace SomeLibrary
    {
       void Log ();
    }

    //MyLibrar.h
    namespace Mine
    {
       bool Log;
    }

    This is exactly what C++ did with the core library it ships with (STL). All the symbols in STL are in the std namespace. This separates the STL from your code and helps avoid conflicts (common in the early days of STL). You can do the same thing for your own apps if you want.

    The problem now becomes how do you reference symbols in a namespace. You cannot simply use the name because it is in a named scope that the compiler won't look in. Instead you have to qualify the symbol with the namespace.

    //App.cpp
    #include "SomeLibrary.h"
    #include "MyLibrary.h"

    void Bar ()
    {
       SomeLibrary::Log();
       Mine::Log = true;
    }

    You have to do this every time you reference the symbol.  Since it is common in a single .cpp file to need to reference a subset of the namespaces in your application then it becomes annoying to have to add the namespace name each time. As a time saver you can instead add a using namespace statement. This basically tells the compiler to go ahead and take all the symbols defined in the referenced namespace and brings them into the global scope (for this source file only).

    //App.cpp
    #include "SomeLibrary.h"
    #include "MyLibrary.h"


    using namespace SomeLibrary;

    void Bar ()
    {
       Log();  //SomeLibrary:: is now redundant

       //Can still reference symbols in other namespaces
       Mine::Log = true;
    }

    #include is pretty much always going to be used in C++ programs as most of the core functionality is defined in header files based upon functionality (i.e. iostream has the input/output routines). Namespaces can be completely ignored if you don't need that functionality.  The only exception is that you'll generally use the std namespace as you'll want to use the STL. Beyond that you could easily write C++ programs without every using namespaces.

    • Marked as answer by scofx Friday, January 16, 2015 2:22 AM
    Wednesday, January 14, 2015 3:10 PM
  • You said include and namespace has no intersection, but in concept at least the two are both meant to call system library. In C++ as you mentioned every file is compiled separately, and include will let compiler know which head file you want to associate, then what is the use for namespace? Because include clause already specified the library author want to use. Still feels a bit duplicated. Can i put it this way, what's the difference between <xxxx.h> in C and <xxxx> in C++, i'm sure these two are not the same thing.

    Neither #include nor using directive "call" anything.

    Some header files are provided by the system (e.g. the C and C++ standard libraries), some are provided by 3rd party vendors (e.g. boost), and some are part of your own application. Any of these types may or may not use namespaces.

    The main purpose of namespaces is to avoid name collisions. When you put a using directive in your code, you are actually subverting this purpose, but you are doing it in a controlled way

    <xxxx.h> and <xxxx> are exactly the same thing, except that the file name is different. For example, <string.h> and <string> are different header files.


    David Wilkinson | Visual C++ MVP

    • Marked as answer by scofx Friday, January 16, 2015 1:57 AM
    Thursday, January 15, 2015 3:01 PM

All replies

  • I know it's kind of basic but really appreciate your help if any
    • Merged by Kristin Xie Tuesday, January 13, 2015 2:30 AM the duplicate thread
    Monday, January 12, 2015 2:57 PM
  • In regards to your question you don't need to add anything to use #include.  #include is handled by the preprocessor (it is a preprocessor directive) and is processed before the compiler every sees your code.  The preprocessor replaces the #include line with the contents of the specified file. The modified source is then sent to the compiler for compilation.

    A using namespace statement lets the compiler know that the symbols contained in the namespace (std in this case) can be used without using the scope operator (::). Normally to use functionality from STL (which resides in the std namespace) you would need to scope the name (i.e. std::string). If you are going to be using the contents of a namespace a lot (such as std) then the using eliminates the need for this.  The compiler adds all the members in the namespace into the current scope so that symbols can be found without the need for the scope operator.

    This forum is for C#, not C++.  I'll move your post.  Please post future questions in the C++ forums.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Monday, January 12, 2015 3:51 PM
  • Is this C# or C++? #include is used in C++ and not C#.

    The using statement in C# specifies which Net libraries are added to your project.  The more Libraries you add the larger the executable will be.  Some Net  libraries are dependent on other libraries so std is required. 


    jdweng

    Monday, January 12, 2015 3:58 PM
  • I know it's kind of basic but really appreciate your help if any

    I am considering that you are aware about namespace concept .What to include and what not to It's depend on your requirement . for example if i am only interested in using cout and cin in that case there is no need to use using namespace std rather you can use using std::cout; & using std::cin etc.

    Thanks


    Rupesh Shukla

    Monday, January 12, 2015 6:12 PM
  • I think i know namespace is a scope to cover some system defined keywords. But there is still doubt within me that what's <iostream> itself? I checked on MSDN but still feels bit obscure. What about you simply declare" using namespace std" and not writing "include<iostream>" then does the compiler still knows what system function you want to call. In C it's quite clear I know <iostream.h> is some head files you want to include in your code but in C++ why you already can point out which namescope you want to use as you write"using namespace std" but still you have to include<iostream>? What exactly is <iostream> then?
    Tuesday, January 13, 2015 2:47 AM
  • Hi scoof,

    Write a simple C++ code in VS.

    Namespace std define in several code sources file included in iosteam.

    You can right click on word "std" and select go to definition. Try the similar operation, you could see how and where namespace std defined. Read the source code should be the best solution to answer your question. 

    define _STD_BEGIN namespace std {

    Best regards,

    Shu Hu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    • Edited by Shu 2017 Tuesday, January 13, 2015 3:22 AM
    Tuesday, January 13, 2015 3:21 AM
  • I think i know namespace is a scope to cover some system defined keywords. But there is still doubt within me that what's <iostream> itself? I checked on MSDN but still feels bit obscure. What about you simply declare" using namespace std" and not writing "include<iostream>" then does the compiler still knows what system function you want to call. In C it's quite clear I know <iostream.h> is some head files you want to include in your code but in C++ why you already can point out which namescope you want to use as you write"using namespace std" but still you have to include<iostream>? What exactly is <iostream> then?

    The file "iostream" is a header file which (like any other) contains class definitions and function declarations, and perhaps some global objects. All the quantities defined or declared in "iostream" (and in other C++ standard library headers) are in the std namespace. Therefore you must refer to the std namespace when using them, either by writing "using namespace std" or by using explicit qualification, like std::cout.

    If you write "using namespace std" without including any C++ standard library headers, you will get an error, because the compiler will not recognize "std" as being the name of a namespace.


    David Wilkinson | Visual C++ MVP

    Tuesday, January 13, 2015 3:38 AM
  • Hi scoof,

    Write a simple C++ code in VS.

    Namespace std define in several code sources file included in iosteam.

    You can right click on word "std" and select go to definition. Try the similar operation, you could see how and where namespace std defined. Read the source code should be the best solution to answer your question. 

    define _STD_BEGIN namespace std {

    Best regards,

    Shu Hu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click  to participate the survey.



    very good suggestions, but can you directly explain to my question, why i have to include iostream to use global stream, why using namespace std or simply denote std before call the system function as for std::cout wouldn't suffice? I know <iostream> in c++ is quite different with <iostream.h> in c which have specific defining of function you are including. what is the purpose of using <iostream>?
    Tuesday, January 13, 2015 3:41 AM
  • I think i know namespace is a scope to cover some system defined keywords. But there is still doubt within me that what's <iostream> itself? I checked on MSDN but still feels bit obscure. What about you simply declare" using namespace std" and not writing "include<iostream>" then does the compiler still knows what system function you want to call. In C it's quite clear I know <iostream.h> is some head files you want to include in your code but in C++ why you already can point out which namescope you want to use as you write"using namespace std" but still you have to include<iostream>? What exactly is <iostream> then?

    The file "iostream" is a header file which (like any other) contains class definitions and function declarations, and perhaps some global objects. All the quantities defined or declared in "iostream" (and in other C++ standard library headers) are in the std namespace. Therefore you must refer to the std namespace when using them, either by writing "using namespace std" or by using explicit qualification, like std::cout.

    If you write "using namespace std" without including any C++ standard library headers, you will get an error, because the compiler will not recognize "std" as being the name of a namespace.


    David Wilkinson | Visual C++ MVP

    Then why some people said include sentence is not used in c#? does that mean in c#, you can simply write using some namespace and compiler will know what to do? And is include just a feature inherited from c language and thus not fully evolved until the release of c#
    Tuesday, January 13, 2015 7:18 AM
  • Then why some people said include sentence is not used in c#? does that mean in c#, you can simply write using some namespace and compiler will know what to do? And is include just a feature inherited from c language and thus not fully evolved until the release of c#

    C# is a completely different language from C++ with a completely different compilation model. It does not use header files, and there is no #include directive.


    David Wilkinson | Visual C++ MVP

    Tuesday, January 13, 2015 11:16 AM
  • davewilk : The using statements in C# and equivalent to include statements in C++.  Both are headers files. Some headers file automatically reference other header files and other don't. I guess you original question about iostream header file probably means the iostream library can be run with and without the std library so it is not included. So std is not in the iostream header.

    jdweng

    Tuesday, January 13, 2015 12:37 PM
  • What about you simply declare" using namespace std" and not writing "include<iostream>" then does the compiler still knows what system function you want to call.

    Fair enough and how compiler will come to know where is this namespace located so you have to include header file.Second as i already mentioned You can also avoid prep-ending of namespaces with the using namespace directive. This directive tells the compiler that the subsequent code is making use of names in the specified namespace. If you can want your own namespace equivalence to count and cin write your own function and if you want that your function should not conflict with some other cout and cin then put them in some namespace or class or struct. Then you don't have to include <iostrem>inside your code.

    Thanks


    Rupesh Shukla

    Tuesday, January 13, 2015 3:51 PM
  • is it just c++'s mechanism to type both <iostream> and indicate specified namespace? I mean it's bit duplicate feels like, why not just state namespace directive and i feel it's clear enough for which system function you want to use. Is it the language is just so designed?

    also, i'm curious if in c#, you could simply state using namespace XXX and compiler will know what you want

    Wednesday, January 14, 2015 3:04 AM
  • Please stop trying to make some sort of association between #include and using namespace. They are 2 completely different concepts for 2 completely different problems.  You might as well say that if you declare a variable called A that you don't need to use an if statement anymore. 

    #include

    • Handled by preprocessor, never seen by the compiler
    • Merges file content together before sending source to compiler
    • Completely optional (although almost always used)

    using namespace

    • Handled by compiler
    • Brings symbols into scope
    • Completely optional (not used in older code bases

    #include is a holdover from C (which didn't have namespaces) and is a consequence of how compilation was done back in the days C was written.  The only thing it does is take the contents of one file and puts them into another. That's it.

    It allows us to break up a large program into separate files for easier maintenance.  You are sort of spoiled with the modern IDEs so it probably isn't clear but the compiler only compiles a single .cpp file at a time. Every .cpp file compiles in isolation. Symbols defined in 1 .cpp file are not visible in another .cpp file as far as the compiler is concerned. 

    //File1.cpp
    void Foo () { }

    The problem then becomes how do you use a function (i.e. Foo) defined in one .cpp file (i.e. File1.cpp) in another .cpp file(I.e. File2.cpp).

    //File2.cpp
    void Bar ()
    {
       Foo(); //Compiler error, no such symbol defined in File2.cpp
    }

    The C++ language requires that a symbol be declared before it is used. As such the compiler requires that Foo be declared before File2.cpp uses it. But since the function is declared in File1.cpp the compiler knows nothing about it.  You could add the declaration to File2.cpp directly but this introduces maintenance issues and can result in a lot of repeated code if you're reusing a lot of functionality.

    //File2.cpp
    //Possible, but not recommended
    void Foo ();

    void Bar ()
    {
       Foo();
    }

    To solve this you tend to put the declarations into a header file. 

    //File1.h
    void Foo ();

    Then both .cpp files can reference it.

    //File1.cpp
    #include "File1.h"

    //File2.cpp
    #include "File2.h"

    void Bar ()
    {
       Foo();
    }

    If you need to change or add a symbol you do it in one place and both files see the changes. Now when the compiler runs it finds the definition of Foo and it can continue.  It is literally no different than if you had hand written the declarations in each of your .cpp files yourself.

    //What the compiler sees - File2.cpp
    void Foo ();

    void Bar ()
    {
       Foo();
    }

    Of course header files can have more than just declarations but the concept is the same. In general a header file name matches the corresponding .cpp file for convenience but the actual name is completely irrelevant other than in the #include line.

    Namespaces solve a completely different problem.  By default declarations are in the global scope.  As programs get larger you start running into name collisions.  The language requires that a symbol be unique in its scope (ignoring function overloading). So as you break up your program into separate files you start to run the risk of collisions. For example you might use a library that has a Log function and you might use another library that manages woodcutters with its own Log function.

    //SomeLibrary.h
    void Log ();

    //MyLibrary.h
    bool Log;

    If both these declarations make it into the same .cpp file (such as via #include) then the compiler will fail as it doesn't know which one to use. 

    //App.cpp
    #include "SomeLibrary.h"
    #include "MyLibrary.h"

    void Bar ()
    {
       Log();  //Compiler error as we don't know which one to use
    }

    To help alleviate that C++ added the concept of namespaces.  A namespace is a simple (hierarchial) grouping of related symbols. It allows you to move symbols out of the global scope and into a named scope. This reduces the likelihood of collisions and organizes related functionality together so it is easier to find.  For example a library might put all its public symbols into a namespace that matches the name of the library (not the file). 

    //SomeLibrary.h
    namespace SomeLibrary
    {
       void Log ();
    }

    //MyLibrar.h
    namespace Mine
    {
       bool Log;
    }

    This is exactly what C++ did with the core library it ships with (STL). All the symbols in STL are in the std namespace. This separates the STL from your code and helps avoid conflicts (common in the early days of STL). You can do the same thing for your own apps if you want.

    The problem now becomes how do you reference symbols in a namespace. You cannot simply use the name because it is in a named scope that the compiler won't look in. Instead you have to qualify the symbol with the namespace.

    //App.cpp
    #include "SomeLibrary.h"
    #include "MyLibrary.h"

    void Bar ()
    {
       SomeLibrary::Log();
       Mine::Log = true;
    }

    You have to do this every time you reference the symbol.  Since it is common in a single .cpp file to need to reference a subset of the namespaces in your application then it becomes annoying to have to add the namespace name each time. As a time saver you can instead add a using namespace statement. This basically tells the compiler to go ahead and take all the symbols defined in the referenced namespace and brings them into the global scope (for this source file only).

    //App.cpp
    #include "SomeLibrary.h"
    #include "MyLibrary.h"


    using namespace SomeLibrary;

    void Bar ()
    {
       Log();  //SomeLibrary:: is now redundant

       //Can still reference symbols in other namespaces
       Mine::Log = true;
    }

    #include is pretty much always going to be used in C++ programs as most of the core functionality is defined in header files based upon functionality (i.e. iostream has the input/output routines). Namespaces can be completely ignored if you don't need that functionality.  The only exception is that you'll generally use the std namespace as you'll want to use the STL. Beyond that you could easily write C++ programs without every using namespaces.

    • Marked as answer by scofx Friday, January 16, 2015 2:22 AM
    Wednesday, January 14, 2015 3:10 PM
  • Please stop trying to make some sort of association between #include and using namespace. They are 2 completely different concepts for 2 completely different problems.  You might as well say that if you declare a variable called A that you don't need to use an if statement anymore. 

    #include

    • Handled by preprocessor, never seen by the compiler
    • Merges file content together before sending source to compiler
    • Completely optional (although almost always used)

    using namespace

    • Handled by compiler
    • Brings symbols into scope
    • Completely optional (not used in older code bases

    #include is a holdover from C (which didn't have namespaces) and is a consequence of how compilation was done back in the days C was written.  The only thing it does is take the contents of one file and puts them into another. That's it.

    It allows us to break up a large program into separate files for easier maintenance.  You are sort of spoiled with the modern IDEs so it probably isn't clear but the compiler only compiles a single .cpp file at a time. Every .cpp file compiles in isolation. Symbols defined in 1 .cpp file are not visible in another .cpp file as far as the compiler is concerned. 

    //File1.cpp
    void Foo () { }

    The problem then becomes how do you use a function (i.e. Foo) defined in one .cpp file (i.e. File1.cpp) in another .cpp file(I.e. File2.cpp).

    //File2.cpp
    void Bar ()
    {
       Foo(); //Compiler error, no such symbol defined in File2.cpp
    }

    The C++ language requires that a symbol be declared before it is used. As such the compiler requires that Foo be declared before File2.cpp uses it. But since the function is declared in File1.cpp the compiler knows nothing about it.  You could add the declaration to File2.cpp directly but this introduces maintenance issues and can result in a lot of repeated code if you're reusing a lot of functionality.

    //File2.cpp
    //Possible, but not recommended
    void Foo ();

    void Bar ()
    {
       Foo();
    }

    To solve this you tend to put the declarations into a header file. 

    //File1.h
    void Foo ();

    Then both .cpp files can reference it.

    //File1.cpp
    #include "File1.h"

    //File2.cpp
    #include "File2.h"

    void Bar ()
    {
       Foo();
    }

    If you need to change or add a symbol you do it in one place and both files see the changes. Now when the compiler runs it finds the definition of Foo and it can continue.  It is literally no different than if you had hand written the declarations in each of your .cpp files yourself.

    //What the compiler sees - File2.cpp
    void Foo ();

    void Bar ()
    {
       Foo();
    }

    Of course header files can have more than just declarations but the concept is the same. In general a header file name matches the corresponding .cpp file for convenience but the actual name is completely irrelevant other than in the #include line.

    Namespaces solve a completely different problem.  By default declarations are in the global scope.  As programs get larger you start running into name collisions.  The language requires that a symbol be unique in its scope (ignoring function overloading). So as you break up your program into separate files you start to run the risk of collisions. For example you might use a library that has a Log function and you might use another library that manages woodcutters with its own Log function.

    //SomeLibrary.h
    void Log ();

    //MyLibrary.h
    bool Log;

    If both these declarations make it into the same .cpp file (such as via #include) then the compiler will fail as it doesn't know which one to use. 

    //App.cpp
    #include "SomeLibrary.h"
    #include "MyLibrary.h"

    void Bar ()
    {
       Log();  //Compiler error as we don't know which one to use
    }

    To help alleviate that C++ added the concept of namespaces.  A namespace is a simple (hierarchial) grouping of related symbols. It allows you to move symbols out of the global scope and into a named scope. This reduces the likelihood of collisions and organizes related functionality together so it is easier to find.  For example a library might put all its public symbols into a namespace that matches the name of the library (not the file). 

    //SomeLibrary.h
    namespace SomeLibrary
    {
       void Log ();
    }

    //MyLibrar.h
    namespace Mine
    {
       bool Log;
    }

    This is exactly what C++ did with the core library it ships with (STL). All the symbols in STL are in the std namespace. This separates the STL from your code and helps avoid conflicts (common in the early days of STL). You can do the same thing for your own apps if you want.

    The problem now becomes how do you reference symbols in a namespace. You cannot simply use the name because it is in a named scope that the compiler won't look in. Instead you have to qualify the symbol with the namespace.

    //App.cpp
    #include "SomeLibrary.h"
    #include "MyLibrary.h"

    void Bar ()
    {
       SomeLibrary::Log();
       Mine::Log = true;
    }

    You have to do this every time you reference the symbol.  Since it is common in a single .cpp file to need to reference a subset of the namespaces in your application then it becomes annoying to have to add the namespace name each time. As a time saver you can instead add a using namespace statement. This basically tells the compiler to go ahead and take all the symbols defined in the referenced namespace and brings them into the global scope (for this source file only).

    //App.cpp
    #include "SomeLibrary.h"
    #include "MyLibrary.h"


    using namespace SomeLibrary;

    void Bar ()
    {
       Log();  //SomeLibrary:: is now redundant

       //Can still reference symbols in other namespaces
       Mine::Log = true;
    }

    #include is pretty much always going to be used in C++ programs as most of the core functionality is defined in header files based upon functionality (i.e. iostream has the input/output routines). Namespaces can be completely ignored if you don't need that functionality.  The only exception is that you'll generally use the std namespace as you'll want to use the STL. Beyond that you could easily write C++ programs without every using namespaces.

    Very impressive work, but beside some differences you listed above, i still think the two, at least in functionality has some overlap. Both are designed to make user code more concise. Is that #include is an old design and is somehow obsolete for today's software designing thus being replaced by new ideas like using namespace? 
    Thursday, January 15, 2015 2:52 AM
  • Very impressive work, but beside some differences you listed above, i still think the two, at least in functionality has some overlap. Both are designed to make user code more concise. Is that #include is an old design and is somehow obsolete for today's software designing thus being replaced by new ideas like using namespace?

    As I told you before, C++ and C# have completely different compilation models.

    In C++ each .cpp file is compiled separately with no knowledge of the contents of other .cpp files. Everything that is needed must be explicitly present. The purpose of the #include mechanism is to avoid duplication and inconsistency. Include files may or may not introduce namespaces.

    In C#, the definitions of all quantities in the project are available to each source file, as well as definitions of quantities appearing in referenced assemblies. There is no need for the include mechanism.

    Both C+ and C# have the using directive, whose purpose in both cases is to avoid having to use the namespace qualifier.

    I really see no relationship between the #include and using concepts. They may be used totally independently.


    David Wilkinson | Visual C++ MVP

    Thursday, January 15, 2015 11:02 AM
  • You said include and namespace has no intersection, but in concept at least the two are both meant to call system library. In C++ as you mentioned every file is compiled separately, and include will let compiler know which head file you want to associate, then what is the use for namespace? Because include clause already specified the library author want to use. Still feels a bit duplicated. Can i put it this way, what's the difference between <xxxx.h> in C and <xxxx> in C++, i'm sure these two are not the same thing.
    Thursday, January 15, 2015 2:18 PM
  • "Both are designed to make user code more concise"

    You are making a huge leap in equivalence here. If you follow this to its logical conclusion then not writing any comments, using single letter variable names and removing all unneeded spacing provides the same functionality as a #include as well.  They are make your code more concise.

    The purpose of #include and using are completely different.  Trying to treat them like an "old" and "new" approach is wrong.  Even in languages that are new these techniques are still used.  Even C# uses this approach (albeit in a different manner for #include).  At the end of the day the #include simply gives the compiler access to code that is not part of the source file being compiled.  The equivalent C# is a reference to an assembly that gets passed to the compiler.  If C++ had implemented a metadata system like .NET then it wouldn't have needed the #include syntax.

    Thursday, January 15, 2015 2:59 PM
  • You said include and namespace has no intersection, but in concept at least the two are both meant to call system library. In C++ as you mentioned every file is compiled separately, and include will let compiler know which head file you want to associate, then what is the use for namespace? Because include clause already specified the library author want to use. Still feels a bit duplicated. Can i put it this way, what's the difference between <xxxx.h> in C and <xxxx> in C++, i'm sure these two are not the same thing.

    Neither #include nor using directive "call" anything.

    Some header files are provided by the system (e.g. the C and C++ standard libraries), some are provided by 3rd party vendors (e.g. boost), and some are part of your own application. Any of these types may or may not use namespaces.

    The main purpose of namespaces is to avoid name collisions. When you put a using directive in your code, you are actually subverting this purpose, but you are doing it in a controlled way

    <xxxx.h> and <xxxx> are exactly the same thing, except that the file name is different. For example, <string.h> and <string> are different header files.


    David Wilkinson | Visual C++ MVP

    • Marked as answer by scofx Friday, January 16, 2015 1:57 AM
    Thursday, January 15, 2015 3:01 PM
  • In sort of namespace is a declarative region of your code to avoid name collision .Why are you confused in this case. #include includes the header you specify and all codes in the global namespace in it. using namespace includes all the codes inside that namespace you specify in all the headers you included.

    Thanks


    Rupesh Shukla

    Thursday, January 15, 2015 3:39 PM