[Rcpp-devel] What is the scope of a "using TYPE;" directive?
    Dirk Eddelbuettel 
    edd at debian.org
       
    Fri Jul 29 23:43:06 CEST 2011
    
    
  
On 29 July 2011 at 16:20, Douglas Bates wrote:
| This is not directly an Rcpp question but it does relate to
| programming style when using Rcpp.
| 
| I try to avoid statements like
| 
| using namespace Rcpp;
| 
| in a header file because that will change the visible names in any
| other files that include this header.  The alternative, of course, is
| many declarations with long names like
| 
| 
|  Rcpp::NumericVector   foo(const Rcpp::IntegerVector&);
| 
| Lately I have taken to including statements of the form
| 
| namespace mynamespace {
|     using Rcpp::NumericVector;
|     using Rcpp::IntegerVector;
| 
|     NumericVector foo(const IntegerVector&);
| }
| 
| so that I control exactly which names are exposed without qualifiers
| and not expose the entire Rcpp namespace.
| 
| Can anyone tell me if that using declaration has effect only within
| the declared namespace "mynamespace"?  That is what I want to have
| happen so that by the end of the header file my temporary exposure of
| names is no longer in effect.
I think that is correct. Namespaces go in scopes just like other declarations
and assignments.  Here is a quick test I just tried:
-----------------------------------------------------------------------------
#include <Rcpp.h>
namespace mynamespace {
  using Rcpp::NumericVector;
  int foo(void) {
    NumericVector a(3);
  }
}
int bar(void) {
  NumericVector b(3);
}
int main(void) {
  return 0;
}
-----------------------------------------------------------------------------
Here bar() has a NumericVector 'out of namespace' and compilation fails,
where it works for foo().  If we make bar() use Rcpp::NumericVector all is
good.  
That should generalise the same way to header which are, after all, handled
by a preprocessor.
Dirk
-- 
Gauss once played himself in a zero-sum game and won $50.
                      -- #11 at http://www.gaussfacts.com
    
    
More information about the Rcpp-devel
mailing list