I am calling a function documented as follows:
int BIO_write_filename(BIO *b, char *name)
with the following:
std::string filename{"myfile"};
if (BIO_write_filename(bio, filename.c_str()) <= 0) {
throw std::runtime_error{"Could not prepare output file.\n"};
}
But I get the following error:
cannot convert argument 4 from 'const char *' to 'void *'
I discovered that I'm actually dealing with a macro:
# define BIO_write_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, BIO_CLOSE|BIO_FP_WRITE,name)
So what I'm actually calling is:
long BIO_ctrl(BIO *bp, int cmd, long larg, void *parg);
What is the best way to call this from C++?
So far I'm considering the following two options:
Option 1: c-style cast:
BIO_write_filename(bio, (void*)filename.c_str())
Option 2: C++ type cast:
const void* filename_ptr_c{(filename.c_str())};
void* filename_ptr{const_cast<void*>(filename_ptr_c)};
BIO_write_filename(bio, filename_ptr)