lundi 26 février 2018

C++ strong-typed enums and calling convention

How safe is it to use callbacks, substituting an int parameter for a strong-typed enum? The callback will be used by a linked library, if that helps.

For instance, if the API / external function looks like this:

using callback_t = void( int );
void set_event( callback_t * );

Then would this approach be safe / well-defined?

enum class result_t : int { /* expected values */ };
using my_callback_t = void( result_t );
// later
void my_callback( result_t );
set_event( &my_callback );

Or would you be required to in cases like this, to write a wrapper callback function just to convert int to result_t?

Aucun commentaire:

Enregistrer un commentaire