'Can I include iostream header file into custom namespace? [closed]
namespace A
{
#include <iostream>
};
int main(){
A::std::cout << "\nSample";
return 0;
}
Solution 1:[1]
You could write:
#include <vector> // for additional sample
#include <iostream>
namespace A
{
namespace std = std; // that's 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;
}
Thit approach is called namespace aliasing. Real purpose for that feature showed in the following sample:
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
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
