jeudi 25 août 2016

How can I concisely assign to the members of a struct depending on a condition?

I have some code that looks like this:

struct mystruct
{
    /* lots of members */
};

void mystruct_init( struct mystruct* dst, int const condition )
{
    if ( condition )
    {
        /* initialize members individually a certain way */
    }
    else
    {
        /* initialize members individually another way */
    }
}

Options I'm considering:

  • Simplest would be to have a function that assigns to every member and call that. Should I simply hope the compiler optimizes that call away?
  • Define a macro to explicitly avoid the function call overhead.
  • Write everything the long way.

What is the proper way to handle such a scenario in C11?

Aucun commentaire:

Enregistrer un commentaire