wiki:OnUsingUsing
Last modified 4 years ago Last modified on 10/24/2014 03:13:02 PM

OBSOLETE

This file has moved to: https://confluence.lsstcorp.org/pages/viewpage.action?pageId=20283856


C++ Using Declaration Convention

from: Software Standards and Policies

Ray's input TBD

C++ provides the using keyword for use in declarations and directives relating to namespaces. This powerful capability can simplify and reduce the verbosity of code, but it can also lead to reliability and maintainability challenges when the source of non-local names is not obvious.

Using-declarations are of the form using N::Class; or using N::function;. These inject a name from a different namespace into the current namespace, where it may be used without qualification.

Using-directives are of the form using namespace N;. These inject all the names in the different namespace into the current namespace.

Coding convention 4-13 bans using using-declarations and using-directives in header files. (It is incorrect about using-directives requiring qualification of names from the different namespace; this may be due to an incorrect analogy with the Python import statement.)

The LSST convention for .cc source files that are not included in header files is:

  • Using-declarations are acceptable and appropriate for use when the class or function is to be referenced repeatedly.
  • Using-directives are to be avoided except where N is std. This convention ensures that unfamiliar names will be properly namespace-qualified at least once in the file.
  • Namespace renaming declarations of the form namespace Short = Long::Qualified::Name; are acceptable to reduce typing.

For example, ....