5

What's the recommended practice regarding the using declaration (e.g using std::vector;)?

Should it be at the top of the cpp/cc file or just in the scope where it's being used?

MikMik
  • 3,426
  • 2
  • 23
  • 41

4 Answers4

2

Limiting its scope would be better in general, but it would take a non-trivial amount of code in your source file to make a difference in practice.

Jon
  • 428,835
  • 81
  • 738
  • 806
2

Chapter 59 of C++ Coding Standards by Sutter and Alexandrescu is named "Don't write namespace usings in a header file or before #include". So they say that you should not write using declaration or using directive before an #include directive, because this may influence the #included code. This also implies that you should not write using in your own header files, cause someone may #include them and this would alter the behavior in the inclusion point (see e.g. some Boost header library).

So don't write using in header files and before #include directive. Feel free to write using in your implementation files after #including directives.

Anton Daneyko
  • 6,528
  • 5
  • 31
  • 59
  • Thanks for this clarification, because I was asking myself about the viabiliy of a piece of code making using below each #include. Like if he was doing an import in java. – daminetreg Feb 03 '12 at 10:32
  • Regarding `using` and headers: avoid even if it's a `using` declaration for a type (not a whole namespace) inside your own namespace to avoid lots of `std::` prefixes? – thomthom Dec 15 '13 at 09:57
  • @thomthom I do not know. They only mention statements like using namespace blah; I guess using blah::type is fine. But I would love to hear from you if you find any confirmation. – Anton Daneyko Dec 16 '13 at 16:47
  • 1
    I found one topic that was briefly mentioning it, but it argued name conflicts if you `using std::vector` and "someone else" defined a `vector` class in your own namespace. However, I don't see how that is any different from if I had defined a custom `Foo` class and "someone else` also tried to define a `Foo` class within my namespace. Will check back if I get more clarifying details on this particular usage. (Though, since it's hard to find it indicated few use it - but I'd still like to know *why*) – thomthom Dec 16 '13 at 17:38
0

If it is in a cpp file it is a matter of style. Some people have a preference to avoid using declarations all together to avoid any ambiguity. If in a header, always have it in at least some scope, otherwise stick to the rule for variables: Always try to have things in the smallest scope as possible.

pmr
  • 58,701
  • 10
  • 113
  • 156
0

For readability its better to have it at the beginning. Otherwise, use it in the smallest scope possible.

To make the code clearer to other, you should avoid using using.

update:

Please take mezhaka's comment into account. I wasn't precise enough, but referred with "at the beginning" to the beginning of the "real" code, i.e. after the #includes's

ezdazuzena
  • 6,120
  • 6
  • 41
  • 71
  • 1
    If you put it in the very beginning, before the #include statements, you would affect the #included files and thus you would change the meaning of the #included code. This might be not what you wanted. – Anton Daneyko Feb 03 '12 at 11:10
  • @mezhaka: Sorry for being unprecise. You are completely right. I referred more to the beginning of the "real" code. – ezdazuzena Feb 03 '12 at 11:12