Jacobian
Jacobian

Reputation: 10842

No match for operator * when using boost::variant

I define my own variant type like so:

typedef variant<myobj **, ... other types> VariantData;

One of my class methods gets this data type as a parameter and tries to do something like:

void MyMethod(VariantData var){
    //method body
    if(some_cond){ // if true, then it implies that var is of type
                   // myobj **
        do_something(*var); // however, I'm unable to dereference it
    }
    // ... ther unnecessary stuff
}

As a result, when I compile my program, I get this error message:

error: no match for 'operator*' (operand type is 'VariantData ....'

I do not know how to fix this error. PS. On the whole the code works well - if I comment out this part related to dereferencing, then everything runs smoothly.

Upvotes: 0

Views: 597

Answers (1)

Igor R.
Igor R.

Reputation: 15075

The error message is quite self-explanatory: you can't dereference boost::variant, it doesn't have such semantics. You should first extract the value, i.e. the pointer, and then dereference it.

To extract the value relying on a run-time logic, just call get():

//method body
if(some_cond){ // if true, then it implies that var is of type myobj **
    do_something(*get<myobj **>(var));
}

Note, however, that if the run-time logic fails (eg. due to a bug), get() will throw bad_get exception.

Upvotes: 1

Related Questions