Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Is this a bad practice? const int sId(int const id); // true/false it doesn\'t m

ID: 658544 • Letter: I

Question

Is this a bad practice?

const int sId(int const id);

// true/false it doesn't matter
template<bool i>
const int sId(int const id) {
    return this->id = id;
}


const int MCard::sId(int const id){
    MCard card = *this;

    this->id = id;

    this->onChange.fire(EventArgs<MCard&, MCard&>(*this, card));

    return this->id;
}


myCard.sId(9);

myCard.sId<true>(8);
As you can see, my goal is to be able to have an alternative behaviour for sId.
I know I could use a second parameter to the function and use a if, but this feels more fun (imo) and might prevent branch prediction (I'm no expert in that field).

So, is it a valid practice, and/or is there a better approach?

Explanation / Answer

Actually, it seems you are just overcomplicating things, which might obfuscate your code. This

sId
and this

sId<true>
are just two different functions. And the standard way of implementing two different functions (with the same signature) is simply to use two different function names, without the template hoodoo voodoo, like sIdWithoutEvent and sId (assumed the latter one is calling the event).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote