[Rcpp-devel] Compiler error I can't understand

Dirk Eddelbuettel edd at debian.org
Sat May 8 14:20:38 CEST 2010


On 8 May 2010 at 11:31, Romain Francois wrote:
| Hi,
| 
| Without seeing the actual code, it looks like a candidate for 
| const_cast. See for example: 
| http://www.cplusplus.com/doc/tutorial/typecasting/

Yes, I briefly hinted the same last eve off-list from my phone.  As
the aforementioned 'C++ Annotations' by Frank Brokken says (now from version
8.1.1 in Debian):

  3.5.2 The ‘const_cast’-operator

  The const keyword has been given a special place in casting. Normally
  anything const is const for a good reason. Nonetheless situations may be
  encountered where the const can be ignored. For these special situations the
  const_cast should be used. Its syntax is:

    const_cast<type>(expression)

  A const_cast<type>(expression) expression is used to undo the const
  attribute of a (pointer) type.

  The need for a const_cast may occur in combination with functions from the
  standard C library which traditionally weren’t always as const-aware as
  they should. A function strfun(char *s) might be available, performing some
  operation on its char *s parameter without actually modifying the
  characters pointed to by s. Passing char const hello[] = "hello"; to strfun
  will produce the warning

    passing ‘const char *’ as argument 1 of ‘fun(char *)’ discards const

  A const_cast is the appropriate way to prevent the warning:

    strfun(const_cast<char *>(hello));


-- 
  Regards, Dirk


More information about the Rcpp-devel mailing list