adding a queue to a class

Started by
11 comments, last by Hodgman 12 years, 8 months ago
std is used with vector, string,list,etc.... so putting std:: everywhere is not very elegant
A standard C++ rule, that's in almost every C++ style guide I've ever read is: Never use a "using namespace" statement globally in a header.

It pollutes the global namespace in the same way that #defines do, which is something that namespaces were designed to avoid.
e.g. this is the problem with defines:#include "windows.h"
#include <algorithm>
int a = 2, b = 4;
int c = std::max( a, b );//compile error - windows contains a #define max line!!!


Now, namespaces solve these kinds of problems for us.
Here is some perfectly valid code:#include <queue>
class queue{};
queue q;//makes my queue, not a std::queue
std::queue<int> q2;//makes a std::queue
Now, if we include a header that has rudely used "using namespace" inside it:#include "Adaline.h" // includes "using namespace std;"
#include <queue>
class queue{};
queue q; // compile error? This code was fine a minute ago?!! thanks Adaline ;)
std::queue<int> q2;


If you really do want to use "using namespace" in a header, then you must at least always wrap it in your own namespace, e.g.//Adaline.h
#include <queue>
namespace adaline
{
using namespace std;//ok, you've only polluted your own namespace
}

Isn't it a bad idea to call a class 'string' anyway ?
The concept extends beyond the [font="Courier New"]std[/font] namespace.
Maybe I'm using two different bits of C++ middleware that both define a Vector3 type? If I using-namespace'd them, I'd get conflicts between the two libraries.
Advertisement

[quote name='Adaline' timestamp='1313396366' post='4849289']std is used with vector, string,list,etc.... so putting std:: everywhere is not very elegant
A standard C++ rule, that's in almost every C++ style guide I've ever read is: Never use a "using namespace" statement globally in a header.

It pollutes the global namespace in the same way that #defines do, which is something that namespaces were designed to avoid.
e.g. this is the problem with defines:#include "windows.h"
#include <algorithm>
int a = 2, b = 4;
int c = std::max( a, b );//compile error - windows contains a #define max line!!!


Now, namespaces solve these kinds of problems for us.
Here is some perfectly valid code:#include <queue>
class queue{};
queue q;//makes my queue, not a std::queue
std::queue<int> q2;//makes a std::queue
Now, if we include a header that has rudely used "using namespace" inside it:#include "Adaline.h" // includes "using namespace std;"
#include <queue>
class queue{};
queue q; // compile error? This code was fine a minute ago?!! thanks Adaline!
std::queue<int> q2;


If you really do want to use "using namespace" in a header, then you must at least always wrap it in your own namespace, e.g.//Adaline.h
#include <queue>
namespace adaline
{
using namespace std;//ok, you've only polluted your own namespace
};

[/quote]
Thanks for you're explanation (I mean it)
No need to be rude anyway
No need to be rude anyway
Sorry if I came across as rude! Just trying to get to the point wink.gif

This topic is closed to new replies.

Advertisement