2

I tried following, it worked without problem, but in this situation, std was not defined.

    using namespace std;

    int main()
    {
       ....
    }
sysiphus
  • 33
  • 2

3 Answers3

9

Your code is illegal. Directive using can only nominate previously declared namespaces, i.e. namespaces whose names can be found by name lookup.

In this case your compiler apparently gives special treatment to name std. It is a compiler-specific extension, which regards std as an implicitly defined namespace. If your try a different namespace name in your code, it will most likely fail to compile.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
2

All using namespace std does is tell the compiler that, when an identifier (named variable, type, etc) appears in code, to look in the std namespace for candidates to match the name.

Within your main() function, no identifiers are being used at all (the {} is an empty block). So there is no need to find matching candidates, and the result of compiling will be the same in both cases.

If you add a statement like cout << "Hello\n" to main(), then the using namespace std will cause the compiler to consider anything named cout within namespace std as a valid match for the identifier cout. If this comes after #include <iostream>, then std::cout is considered a viable match. If declarations in <iostream> are not visible to the compiler (in your case, because there is no #include <iostream>) then the compiler cannot consider std::cout to be a candidate match for cout, and will issue a diagnostic accordingly.

The C++ standard does not specify anything different about std or about using directives that require using namespace std and using namespace othername to behave differently. If your compiler treats them differently (recognising namespace std but not others, even if there is no declaration of one) then that is specific to your compiler.

Peter
  • 35,646
  • 4
  • 32
  • 74
  • Before we "using namespace something", we need declare "something", but "std" needn't(just like the examplt), why? – sysiphus Jun 04 '17 at 04:19
  • The C++ standard does not specify anything different about `std` or about using directives that require `using namespace std` and `using namespace othername` to behave differently. If your compiler treats them differently, then that is specific to your compiler. I'll edit to include a note about that shortly. – Peter Jun 04 '17 at 04:44
  • Would "std::type_info" and "std::initializer_list" be forward declared (though not necessarily complete) even without any includes? If so, the "std" namespace would have to exist. – David A Jun 05 '17 at 14:15
  • The compiler having visibility of a type in a header that is not included would, logically, break the philosophy of keeping "core language" and "library" separate, so I'd be surprised if the standard would require it. A program is ill-formed if it uses `typeid` without a preceding `#include `. A brace-init-list (construct like `{a,b,c}`) is bound to `auto`, which means its type can be anonymously deduced by the compiler and suggests the compiler wouldn't need to see a declaration of `std::initializer_list` to deduce the type. – Peter Jun 05 '17 at 15:55
  • @David ["The template std​::​initializer_­list is not predefined; if the header is not included prior to a use of std​::​initializer_­list — even an implicit use in which the type is not named — the program is ill-formed."](http://eel.is/c++draft/dcl.init.list#2) – cpplearner Jun 06 '17 at 15:27
-4

Try and do std::cout, it won't work.

using namespace std just defines the namespace

#include {x} 'includes' the code within the namespace

I.e, std::cout would need #include <iostream>. Very simple explanation but it's what is going on.

Polymer
  • 1,108
  • 1
  • 9
  • 17
  • But when i try "using namespace other(anything except std)", it won't work because i didn't define "other", compiler helps it? – sysiphus Jun 04 '17 at 04:00
  • Simply because by default the C++ program will already have the 'std' included from the compiler – Polymer Jun 04 '17 at 04:04