Tiutto
Tiutto

Reputation: 199

invalid conversion from 'void* - casting

I have an issue with a code that compiles without problem under gcc but not with g++. I am moving to g++ since I would like to make use of boost for some multithreading improvement. The errors I get are something like:

invalid conversion from 'void* volatile' to 'TrialVect*'

the piece of code is:

static void funcSize( struct TrialVect *vector ) {
  if ( vector->field ) {
    struct TrialVect *ptr = BO2V (vector->first,vector->second);
   }
}

As I googled there is some issue with casting variables, why? suggestions how to fix it?

Upvotes: 1

Views: 1786

Answers (1)

Jonathan Potter
Jonathan Potter

Reputation: 37122

In C++ any pointer can be converted to void* implicitly, but converting from void* requires an explicit cast.

Try this:

auto ptr = static_cast<TrialVect* volatile>( BO2V (vector->first,vector->second) );

volatile is an attribute (like const) that must be handled separately; either the new pointer must match the old one, or you can change the const/volatile attributes using a separate const_cast. For example,

auto ptr = static_cast<TrialVect*>( const_cast<void*>( BO2V(vector->first, vector->second) ) );

Whether removing the volatile attribute is safe or not depends on the application.

Upvotes: 2

Related Questions