DropDropped
DropDropped

Reputation: 1273

C++ callback function which does nothing

I have a code like this

typedef void(_stdcall * MyProcessor)(int, int);

void FunctionProcess (MyProcessor process){
    MyProcessor myCallback;
    myCallback = (process != NULL)? process:"<functionThatDoesNothing>";
    ...
}

If there won't be any callback function in the argument, I'd like to assign some function to myCallback, which would do nothing (or little something), because afterwards, I'm calling this function in loop (and I'd like to avoid 'if' in the loop because of the pipeline flush). I've tried a no-op lambda with no success (incompatible).

Are there any functions like this? Are there any other possibilities? Thank you.

Upvotes: 1

Views: 1183

Answers (3)

Qnan
Qnan

Reputation: 3744

Write an empty function with the signature you want.

void __stdcall pass(int a, int b) 
{
}

Upvotes: 1

bitmask
bitmask

Reputation: 34644

typedef void(_stdcall * MyProcessor)(int,int);

void _stdcall noop(int,int) {}

void functionProcess (MyProcessor process){
    MyProcessor const myCallback = process?process:noop;
    myCallback(7,4);
}

Upvotes: 0

David Schwartz
David Schwartz

Reputation: 182829

Your logic is insane! If the if is correctly-predicted it will be nearly free. If the indirect jump is incorrectly predicted, it will be horrible. An if is much easier to predict than an indirect jump (only two possibilities, speculative execution is possible, there are more prediction slots on most CPUs). So there are pretty much no conceivable circumstances where this makes sense.

Upvotes: 5

Related Questions