Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How important is consistent usage of using declarations?

Most of the research I've done on the use of using declarations, including reading relevant sections of various style guides, indicates that whether or not to use using declarations in C++ source files, as long as they appear after all #includes, is a decision left to the coder. Even the style guides I read, which usually come down on one side or the other of such common disputes for the sake of consistency, are fairly flexible in this regard.

My question is, given this high degree of flexibility, how important is it to use a consistent style? For example, suppose an author wrote something like

using std::vector;

vector<T> v;

std::cout << v[0] << std::endl;

Is the inconsistent application of using on std::vector but not std::cout or std::endl generally considered acceptable, or would it be considered undisciplined?


2 Answers

I think the whole point of using is that you use it inconsistently among names. Names you need very frequently in some block can be declared locally with a using declaration, while others are not. I don't see a problem with that.

Declaring a name to have namespace scope is always much harder to take. I think if the name clearly is known to belong to a particular namespace so that confusing it with other namespaces won't occur, It won't hurt to put a using declaration if it makes your code more readable.

like image 78
Johannes Schaub - litb Avatar answered Jul 25 '26 22:07

Johannes Schaub - litb


I am now a strong proponent for explicitly stating the namespace (ie no 'using')

Most peoples namespace history goes like this (in non trivial, >100kloc projects)

Innocence -> style 1

using namespace std;

Ouch -> style 2

using std::string;
using std::vector;

OK, enough already -> style 3

std::string foo = "xxx";
like image 26
pm100 Avatar answered Jul 25 '26 22:07

pm100