Re: #include and namespaces
On 3/1/2012 2:20 PM, Anand Hariharan wrote:
On Mar 1, 2:34 am, Luca Cerone<luca.cer...@gmail.com> wrote:
Dear all,
I've a header mylib.h
that contains the line
using namespace std ; // before the declaration of my namespace
//and functions.
I put this because I'm declaring several functions that use vectors
and strings and I'd like not to write std:: in front of every of such types.
Everything works fine, but there is a small issue:
As others have said, it is a bad idea. What is a small issue now,
will become a bigger issue as your program grows as well as when the
library grows with more names introduced into the std namespace.
If you use only vector or string, you could do --
using std::string;
using std::vector;
While the name 'string' being introduced into all your C++ source
files should not cause a problem, introducing the name 'vector' could.
The simplest solution is to do a search-replace of string or vector
with std::string or std::vector in your header file and you should be
able to resolve your problem.
For toy projects I consider it totally acceptable to even do
typedef std::vector<int> vecint;
typedef std::vector<double> vecdbl;
typedef std::string str;
_in a header_ and use 'vecint', 'vecdbl' and 'str' afterwards. However,
it all hinges on the definition of "a toy project".
V
--
I do not respond to top-posted replies, please don't ask