woessner@gmail.com wrote:
Quote:
Hi all. This isn't a really pressing question, but it has been
kicking around my head for a while. Is there any major difference
between the following functors:
>
template<typename T, T m_kTarget>
struct SEqual1
{
bool operator()(const T& krElem) const { return(krElem == m_kTarget);
}
};
>
template<typename T>
struct SEqual2
{
Equal2(const T& krTarget): m_kTarget(krTarget) {}
|
SEqual2 ...
Quote:
>
bool operator()(const T& krElem) const { return(krElem == m_kTarget);
}
>
private:
const T& m_kTarget;
};
>
Obviously, SEqual1 can't be used with non-primitive types. But beyond
that, is there any reason to prefer one over the other? I'm thinking
SEqual1 will run faster but may cause code bloat due to many
instantiations.
|
Why would it run faster? Yes, a use of an immediate value *could*
be better than a [reference to a] data member, but you have to prove
that (a) it's actually faster on your system and (b) that it matters
in the context of your program.
Quote:
This is a pretty trivial example, but I've come across situations like
this a couple of times. I think I almost always go with SEqual2, but
I'm really starting to wonder if I should consider the alternative.
|
Even if you should, you wouldn't know by guessing. Only by measuring.
What you might consider is a specialisation of SEqual2 for integral
types. Bewhare, though, that you will only be able to use those with
literals. That alone is too severe a limitation for me.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask