473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Functor Parameters: Template vs. Members

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) {}

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.

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.

Thanks in advance,
Bill

Dec 5 '06 #1
4 1344

wo******@gmail.com wrote:
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) {}

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.

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.

Thanks in advance,
Bill
I think it is more like how you want to use the functor. I think
Vandevorrde's book has a pretty big section about the usage of functors
in this manner.

Having said that, if I want to preserve some state and possible that
state can change between calls to the function operator, then I would
use the second one.

First one might be better but as you pointed out causes a code bloat
for every value of that type you have uses it for.

Dec 5 '06 #2
wo******@gmail.com wrote:
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 ...
>
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.
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
Dec 5 '06 #3
am******@gmail.com wrote:
wo******@gmail.com wrote:
>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) {}

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.

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.

Thanks in advance,
Bill

I think it is more like how you want to use the functor. I think
Vandevorrde's book has a pretty big section about the usage of
functors in this manner.

Having said that, if I want to preserve some state and possible that
state can change between calls to the function operator, then I would
use the second one.
The second one does not preclude the existence of state. It's the
manner in which it accepts the value against which to compare. The
former you have to instantiate using the template argument, the latter
you instantiate using the constructor argument.
First one might be better but as you pointed out causes a code bloat
for every value of that type you have uses it for.
If it's really as thin as the one presented here, does it matter?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 5 '06 #4

wo******@gmail.com wrote:
template<typename T>
struct SEqual2
{
Equal2(const T& krTarget): m_kTarget(krTarget) {}

bool operator()(const T& krElem) const { return(krElem == m_kTarget);
}

private:
const T& m_kTarget;
};
Is there some reason that you're using a T reference instead of just a
T? Maybe just a holdover from wanting to be able to use that for
classtypes?

If you have reason to believe that SEqual would be faster, it might be
worth trying this as an option too; it'll probably be between the two.
(But as Victor said, don't just assume.)

Evan

Dec 5 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Alex Vinokur | last post by:
Functor-parameters in the for_each() and transform() algorithms are passed by value. Might it make sense to have also algorithms for_each2() and transform2() which pass Functor-parameters by...
3
by: CoolPint | last post by:
I have implemented a generic priority queue below and tested it works fine, but I have one small problem I cannot understand. I have type parameter F which determines the priority so that users can...
3
by: Gianni Mariani | last post by:
I was a little surprised by this: It seems like the code below should not compile but the Comeau 4.3.3 compiler accepts it and the gcc 3.4(prerel) compiler rejects it and MSVC++7.1 ICE's. ...
8
by: Amit | last post by:
Hello all. If I want to use an object both as a Functor and also, if I pass a function pointer, how can it be done ? For instance, I have something like this template< typename Iter, typename...
8
by: daniel.w.gelder | last post by:
Hello, I have been trying to write a functor template for a week now and I'm just having tons of trouble because I don't understand an issue that I guess is pretty basic to this task. ...
4
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)>...
12
by: aaragon | last post by:
Hi everyone, I'm trying to provide some external functionality to a class through a functor object defined by the user. The concept is as follows: template <class Functor> class ClassA {...
3
by: alan | last post by:
Hello all, I'd like to know if there is a nice method of defining a functor creator which accepts an N-ary function and returns a functor based on that function. For example I have a function:...
2
by: aaragon | last post by:
Hi guys, Is there a way to return a functor from a recursive call that takes different paths? Let's say that I have a tree structure like: root | first child ---- nextSibling ----nextSibling...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.