Use a template to get alternate behaviour?

Posted by Serge on Programmers See other posts from Programmers or by Serge
Published on 2014-06-09T10:06:47Z Indexed on 2014/06/09 15:41 UTC
Read the original article Hit count: 344

Filed under:
|
|

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?

© Programmers or respective owner

Related posts about c++

Related posts about templates