std namespace - finer partitioning?

  • Thread starter Thread starter Peter
  • Start date Start date
Joined
3/24/08
Messages
63
Points
18
Dunno if this is the right forum, but the question sprung into existence as a direct consequence of HW #1, so perhaps this is the right venue for a programmy type question.

"using namespace std;" brings in all kinds of stuff into the global namespace, ranging from file readers to iterators, to newline characters to vectors to bessel functions.

When using the Boost library, using the std namespace will make vector declarations ambiguous, since we have:

std::vector
boost::numeric::ublas::vector;

It's not really a big deal at all, since a using directive (or explicit reference) makes things unambiguous:

Code:
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>

using namespace boost::numeric::ublas;
using namespace std;


int main ( void )
{
   using boost::numeric::ublas::vector; // Use Boost's vector, not STL's.

   vector<complex<double> > v(3);
    . . .
I'm guaranteed that v is a "Basic Linear Algebra Subprograms" vector, and not a "Standard Template Library" vector. But this is inelegant.

Namespaces in C++ can be nested. I was wondering if the std namespace was partitioned any finer than on the ... whole universe level. For numerical calculations we're mostly interested in cin, cout, and endl to interact with our programs. So I'm wondering if there's something like:

using namespace std::consoleIOstuff;

which would bring cin, cout, endl into the global namespace, but leave things like std::vector and gudermanian functions out of the global namespace. Spent 10 minutes Googling, but couldn't find any such thing.

Implicit to all this is my desire to not be bothered with typing

using std::foo;
using std::bar;
using std::foobar;
using std::barfoo;

in all my programs. I really just want one clean declaration that imports all IO related stuff into the global namespace, and nothing else.
 
Nope, the standard is the standard. That's part of the language. However, you can specify the std namespace everywhere you need it (std::cin, std::cout, etc) and you don't have to include it.
 
Nope, the standard is the standard. That's part of the language

That's what I was afraid of. Rats. Thanks for confirming!

However, you can specify the std namespace everywhere you need it (std::cin, std::cout, etc) and you don't have to include it.
I'd rather not use "using std::foo" for each symbol not in the global namespace.

It's so odd --- the namespace concept is so powerful and useful, but the standard basically ignores its full potential with regards to std. Just imagine how many less lines of code there would be in the world if developers didn't have to type

Code:
// Polluting the global namespace
There would be SO much less code in the world to maintain! ;-)
 
t's so odd --- the namespace concept is so powerful and useful, but the standard basically ignores its full potential with regards to std.

Actually, the concept of namespaces came late into C++ and it was meant for name resolution. Typing more code is ok as long as it is clear and people can understand the code. The standard doesn't ignore its full potential. It was meant to be like that in first place.
 
Why not typedef?

Edit: Presuming, that is, that you are prepared to lose the template functionality of your proposed vectors :(

e.g.

typedef std::vector<int> svectorint;
typedef boost::numeric::ublas::vector<int> bvectorint;

At least then you're being concise but explicit, albeit at the loss of template arguments...

If you then make sure not to have "using namespace" anywhere, then use of vector directly will throw a compiler error as a helpful reminder to be explicit ;)


This is of course useless if you want to retain the template. *sigh* Bad C++. Dirty C++. In your bed.
 
Back
Top Bottom