Can i include iostream header file in user namespace?

namespace A
{
   #include <iostream>
};

int main(){
 A::std::cout << "\nSample";
 return 0;
}
+3
source share
2 answers

Short answer: None.

Long answer: Well, not really. You can fake it. You can declare it outside and use it with operators inside the namespace, for example:

#include <iostream>

namespace A
{
   using std::cout;
};

int main(){
 A::cout << "\nSample";
 system("PAUSE");
 return 0;
}

You cannot localize the library, because even if it had access to A, it would not have access in the standard namespace.

In addition, "Another problem is that the qualified names inside the namespace will be A :: std :: cout, but the library will not contain names corresponding to the external namespace." thanks Jonathan Leffler.

, , , , cpp iostream . ( - ), , .

+8

:

#include <vector> // for additional sample
#include <iostream>
namespace A
{
  namespace std = std; // that ok according to Standard C++ 7.3.2/3
};

// the following code works
int main(){
 A::std::cout << "\nSample"; // works fine !!!
 A::std::vector<int> ints;
 sort( ints.begin(), ints.end() );  // works also because of Koenig lookup

 std::cout << "\nSample";  // but this one works also
 return 0;
}

. :

namespace Company_with_very_long_name { /* ... */ }
namespace CWVLN = Company_with_very_long_name;

// another sample from real life
namespace fs = boost::filesystem;
void f()
{
  fs::create_directory( "foobar" );   // use alias for long namespace name
}
+5

Source: https://habr.com/ru/post/1713916/


All Articles