[Rcpp-devel] Using namespaces to improve code readability

Douglas Bates bates at stat.wisc.edu
Fri Dec 17 13:26:08 CET 2010


On Thu, Dec 16, 2010 at 5:18 PM, Savitsky, Terrance <savitsky at rand.org> wrote:
> I note that employing the namespace declaration -  using namespace arma; -
> allows one to use the declaration of a new armadillo matrix under an
> RcppArmadillo header with – mat X(Xr.begin() …. – instead of – arma::mat
> X(Xr.begin() …

> Do you not recommend this approach due to possibility for overlapping class
> names between Rcpp and armadillo?

If there are name clashes it is always possible to use fully qualified
names to disambiguate the choice.  That is, arma::mat can be used even
after a

using namespace arma;

declaration.  However I don't think there are name clashes between
Armadillo and Rcpp at present.

One recommendation that many C++ programmers miss is that when you
separate the declarations of classes, etc. from the definition, by
writing both a foo.h and a foo.cpp file, it is not a good idea to have
a using namespace declaration in the header file, foo.h, because of
downstream consequences.  If another programmer includes your header
file to get the declarations of your classes etc. then their name
resolution will be changed by your using namespace directive, which is
not a good idea.  The using namespace declaration should be used in
the .cpp file only.


More information about the Rcpp-devel mailing list