mercredi 12 février 2020

Use class name for conditional macro expansion

I have a function A whose code changes a bit depending on if the input is a struct B or a struct C. I don't want to write A twice, so I encapsulated the difference in the () operator of the structs.

My problem is that A processes a lot of data, and the () operator is called extensively. I would like to replace that part with macros to improve the performance of my code. Something like this:

#define B_TASK(data) //macro for B's task
#define C_TASK(data) //macro for C's task

struct B();
struct C();

template<class struct_t>
function A(struct_t data){
     //some irrelevant code
     .
     .
     while(..){ //extensive loop
         #if data == B //my original code uses data()
            B_TASK(data)
         #else
            C_TASK(data)
         #endif
     }
}

int main(){
    B b_inst;
    C c_inst;

    A<B>(b_inst);
    A<C>(c_inst);
}

Is it possible to do something like this? I don't know how to ask for the struct name in the preprocessor #if.

Aucun commentaire:

Enregistrer un commentaire