views:

277

answers:

4

I'm working on a cross platform project that uses STL. The other compiler includes STL support by default, but in VS2005 I need to add the following before the class definitions that use STL items:

#include <cstdlib>
using namespace std;

Is there a VS2005 option that would set this automatically? It's just a bit tedious to work around. I'm just trying to avoid lots of #ifdefs in the source -

EDIT: The other compiler is the IAR workbench for the ARM 926x family. Perhaps I should get them to explicitly do the includes?

Also - is "std::map<>" preferred over "using namespace std; map<>" ?

+1  A: 

Try refering to STL components by their namespace-qualified name (i.e. std::vector).

Doing a global 'using namespace std' is usually a bad idea.

Or maybe I'm not understanding the question.

+3  A: 

All compilers should require you to include those lines. If they don't, then they're just encouraging you to write non-portable code because you're relying on certain headers to be included automatically and you're relying on certain names to be in scope implicitly.

I don't mean to say that those two lines should always be required, though. I only mean that if the rest of your code is written to use things declared in the cstdlib header and in the std namespace, then those two lines need to appear first, and the compiler shouldn't act as though they are there when they really aren't.

Check whether your other compiler has some settings to disable this implicit code. If it doesn't, then it's probably a very, very old compiler, and you should consider not using it and not supporting it anymore.

Rob Kennedy
A: 

The IAR compiler does not support the std namespace (I'm not sure why, because it does support namespaces in general if I remember right).

If you look in the runtime headers for IAR you'll see that they do some macro gymnastics to work around that (the runtime is licensed from Dinkumware, who provide runtimes for quite a few compilers).

You may need to do something similar if you want your stuff to work in multiple environments. A possible cleaner alternative is to just include the "using namespace std;" directive. I might be wrong, but I think the IAR compiler essentially ignored it (it didn't mind that you were using a namespace it didn't know about). A lot of people will think that's ugly, but sometimes you gotta do what the compiler you have wants you to do.

Michael Burr
A: 

In general you should avoid "using namespace X", especially in header files (because everyone who includes your header gets that namespace too whether they want it or not), and especially for namespace std (because it's so big and the potential for name collisions is big).

Instead, in header files refer to names by their fully qualified form, e.g.:

// for plain functions
void foo(std::map<int> intMap);

// for classes
class person {
    std::string name_;
  public:
    person(std::string name);
    // ...
};

Then, in code files, you can do "using", but prefer using specific items in the namespace rather than pulling in the entire namespace. e.g.:

using std::map;
using std::string;

void foo(map<int> intMap) { ... };
person::person(string name) : name_(name) { ... };

etc. This way you avoid impacting others including your headers, and you avoid pulling in potentially zillions of names that might cause a collision with other stuff.

Mike Kale