[Rcpp-devel] typedef and const

Romain Francois romain at r-enthusiasts.com
Thu May 20 15:23:33 CEST 2010

Le 20/05/10 15:07, Douglas Bates a écrit :
> In the process of converting code to use the Rcpp classes I discovered
> that a combination of typedef and const does not work as I expected it
> to work.
> The background is that the C-based sparse matrix library called
> CHOLMOD (used in the Matrix package for R) declares a number of C
> structs such as cholmod_sparse, cholmod_dense, ...  Most of the
> CHOLMOD functions take pointers to these arguments and return pointers
> to freshly-allocated structures.  For brevity I declared typedefs like
> typedef cholmod_sparse *CHM_SP;
> I had thought that I could replace an argument declaration like
> const cholmod_sparse *A
> by
> const CHM_SP A
> but discovered after much experimentation that const CHM_SP A expands to
> cholmod_sparse *A
> In other words, I can't use const to modify a typedef as I expected.
> Can someone explain why?  Is there an alternative declaration that
> accomplishes what I want?  For the time being I have reverted to the
> full declaration of const cholmod_sparse*

In STL, you often see things like :

       typedef void*       pointer;
       typedef const void* const_pointer;

so I suppose you cannot combine const and typedef.

If you only want convenience, maybe you can use macros instead :

#define CHM_SP cholmod_sparse *


Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
|- http://bit.ly/bklUXt : RcppArmadillo 0.2.1
|- http://bit.ly/936ck2 : Rcpp 0.8.0
`- http://bit.ly/9aKDM9 : embed images in Rd documents

More information about the Rcpp-devel mailing list