::std conversion

I want to start migrating to the std:: in place of 'using namespace standard'. What is a good reference or guide to tell me what aspects of my current programming need to be identified as std:: instead of"using namespace std". Search engines have a lot on why is "namespace bad", avoiding name collision with other libraries, or other, but no ready down and dirty guide to take some average code..

Last edited on
It's anything which you had to put a #include <library-name> statement, where library name doesn't have a file extension, is I think the best way of clarifying it. e.g. if you want to use a string (std::string) in your code, you #include <string>, but if you want to use SFML, as an arbitrary example, you put #include <SFML/Graphics.hpp> because it is not part of the standard library, and so doesn't require std::. The other way of doing it is to simply delete using namespace std and your compiler will tell you that it doesn't know what iostream, string, vector etc are, so you know you have to put std:: in front of them.

EDIT: I have just found out that library makers can use no extension, although it is rare. Perhaps a better solution would either be to google C++ standard library, or to think that any #include <library-name> where you haven't had to link an external library is (as far as I know) is a standard library, so you need std::, but your compiler will tell you if you get it wrong.
Last edited on
Somewhat on topic: http://www.gotw.ca/gotw/053.htm


Qt shifted from <qstring.h> to <QString> header name convention on Qt-4. There is no strict rules about filenames, but editors and build tools prefer certain extensions. (Systematic names are easier to automate on.)
Just remove the "using namespace std" from a file and try to compile it. You'll get a bunch of "undeclared symbol" errors and those are the ones you need to fix.

If the symbol is in a header file, add the std:: prefix. If the symbol is in the a code file, you can add using std::symbol; after all the #includes . The point here is that you don't want to have any using statements in your header files because that using will carry over to any header files that get included after yours.
If I let the compiler complain about not using namespace std and I am receiving errors from the code I am currently writing, it seems like a lot of extra work. I would guess though this becomes arbitrary after you have been migrating for a while (doing it correctly all along).It becomes second nature.

*The other option is to 1) finish the coding like usual until its clean w/using namespace std...intact, then 2)pull out the using namespace std, compile and find the symbol errors and implement std::. This would allow for more disambiguation in that compiler errors and std:: errors are seperated..
Last edited on
Yes that's what I meant. I thought the code was compiling cleanly with using namespace std.
What is a good reference or guide to tell me what aspects of my current programming need to be identified as std:: instead of"using namespace std".


The thing to realise here is that the whole of the STL is in std::.

So to start with that would be anything to do with a stream, container or algorithm.

Also, if you use something like std::string say, then you should be looking at the documentation for it: It will tell you what file to include, and whether it is in std. cppreference does that, it's examples always show std::.

The other thing with namespaces is that one should put their own code into it's own namespaces:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// MyStruct.hpp file
namespace MyNameSpace {

  struct MyStruct {
     int Indigo;
     double Fred;
  };
} // end ns MyNameSpace


// A .cpp file
#include "MyStruct.hpp"

//namespace alias
namespace mns = MyNameSpace;

// refer  to the struct or class via the namespace, just like doing std:: , 
// similar idea for calling a function that is inside the struct or class
mns::MyStruct.Fred = 10.0;



Some other libraries have nested namespaces, like boost does for instance. So one does this:

namespace bg = boost::geometry;

Then refer to a boost geometry thing with bg::
Last edited on
Yes I have used namespaces in .h files rusty though.
Topic archived. No new replies allowed.