Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use std:: and boost:: prefixes everywhere?

Tags:

c++

In my C++ code I don't use the declarations using namespace std; or using namespace boost;. This makes my code longer and means more typing. I was thinking about starting to use the "using" declarations, but I remember some people arguing against that. What is the recommended practice? std and boost are so common there should be no much harm in that?

like image 658
quant_dev Avatar asked Jul 12 '09 09:07

quant_dev


2 Answers

I use using namespace only in C++ files, not in headers. Besides, using hole namespace not needed in most of times. For instance, you could write using boost::shared_ptr or using std::tr1::shared_ptr to easily switch between shared_ptr implementations.

Sample:

#include <iostream>

using std::cout;

int main()
{
    cout << "test" << std::endl;
    return 0;
}
like image 128
Kirill V. Lyadvinsky Avatar answered Sep 19 '22 02:09

Kirill V. Lyadvinsky


Using namespace ... was not invented just for fun.

If you have a good reason to use it, then do so (and the frequent use of stuff from these namespaces is the exact good reason). Don't listen to fanatics who tell you everything they don't want to do for obscure reasons themselves is evil.

However, a good source for reasoning in these regards is C++ FAQ lite: http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

I have read it and still decided to use it like you want to. Now you can make your own informed decision :-)

like image 36
ypnos Avatar answered Sep 18 '22 02:09

ypnos