How important is consistent use of declarations?

Most of the research I have done on using declarations, including reading the relevant sections of various style guides, indicates whether to use declarations in C ++ source files if they appear after all #includes, this decision remains for the encoder. Even the stylish manuals that I read, which usually go down from one or the other of such common disputes for the sake of consistency, are quite flexible in this regard.

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

using std::vector;

vector<T> v;

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

Is an inconsistent use of using on std :: vector, but not std :: cout or std :: endl generally considered acceptable or considered undisciplined?

+3
source share
5 answers

I think the whole point usingis that you use it inconsistently among names. Names that you very often need in a block can be declared locally with a using declaration, while others cannot. I do not see a problem with this.

. , , , , . , .

+7

(.. no 'using')

( > 100kloc)

→ 1

using namespace std;

Ouch → style 2

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

, → 3

std::string foo = "xxx";
+2

, using namespace std; , , . , , std:: qualifier --- , "std::vector" 20 , , " std::vector". .

"std::", , , .

+1

using ( ).

typedefs, :

typedef std::vector< int > IntVector;
typedef std::vector< Foo > FooVector;
0

, , . . , , , , .

. T, . swap(T &, T&), , template <class T> std::swap .

swap, , - , , , , std::swap , ​​ .

using :

using namespace std;

template <class T>
mysort(/* ... */ ) {

    // ...

    if (less(x[a], x[b])
        swap(x[a], x[b]);

    // ...
}

, , T, swap(T &, T&), . , std::swap ( ), using namespace std; .

, , using namespace x; . , . , , . , , , , , - .

, using namespace x;, , , . , , , , , , .

, , , , , , , , - .

0

Source: https://habr.com/ru/post/1766827/


All Articles