Public Group

# "..." for templates?

This topic is 3618 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

It can get annoying sometimes having to do reinstances of functions like...
		//========================================================================

/**
* Performs a method on each of the objects in the container.
*/
void MethodBatch(void (T::*_Method)()) {
for(list<T*>::iterator i = m_ObjectList.begin(); i != m_ObjectList.end(); i++)
((*i)->*_Method)();
};

//========================================================================

/**
* Performs a method on each of the objects in the container.
*/
template<typename Arg1> void MethodBatch(void (T::*_Method)(Arg1), Arg1 _Argument1) {
for(list<T*>::iterator i = m_ObjectList.begin(); i != m_ObjectList.end(); i++)
((*i)->*_Method)(_Argument1);
};

//========================================================================

/**
* Performs a method on each of the objects in the container.
*/
template<typename Arg1, typename Arg2> void MethodBatch(void (T::*_Method)(Arg1, Arg2), Arg1 _Argument1, Arg2 _Argument2) {
for(list<T*>::iterator i = m_ObjectList.begin(); i != m_ObjectList.end(); i++)
((*i)->*_Method)(_Argument1, _Argument2);
};

//========================================================================

/**
* Performs a method on each of the objects in the container.
*/
template<typename Arg1, typename Arg2, typename Arg3> void MethodBatch(void (T::*_Method)(Arg1, Arg2, Arg3), Arg1 _Argument1, Arg2 _Argument2, Arg3 _Argument3) {
for(list<T*>::iterator i = m_ObjectList.begin(); i != m_ObjectList.end(); i++)
((*i)->*_Method)(_Argument1, _Argument2, _Argument3);
};


Is there any "..." that can be used for templates? Thanks in advance, GanonPhantom

C++0x.

##### Share on other sites
For your problem, you might just try binding the function you want to execute to a boost::function<> and just invoke it with std::for_each

##### Share on other sites
Possible solutions:

* Variadic templates. Tailor-made for your problem. And when compilers start supporting C++0x, probably sometime around 2015, that'll totally rock for you.

* Boost::preprocessor. A little fiddly to get working, but also tailor-made for this. Just don't have any typos.

* Typelists. Doesn't actually work for this situation, but it's an important tool in the "arbitrarily number of things in a template" toolbox.

* Allow only unary free functions (which operate on the object by reference). RDragon suggested boost::function, which is useful if you want to do it at runtime, but even that's unnecessary if the function is known at compile time. Just take a functor and call it. If you need boost::bind to build that functor, you know where to get it.

##### Share on other sites
Well, the project im working on, needs to stick to the current standard, and im not really "huncky dory" about boost (because of the "sticking to the standard" thing). So i guess for now im stuck with repeating overloaded functions.

##### Share on other sites
Quote:
 Original post by GanonPhantomWell, the project im working on, needs to stick to the current standard, and im not really "huncky dory" about boost (because of the "sticking to the standard" thing). So i guess for now im stuck with repeating overloaded functions.

Boost is implemented using standard C++.

All you're doing by writing your own solution is reinventing what already exists in boost, except it will be less generic, and all-around less useful, not to mention it's more code you need to maintain and debug.

Avoiding educating yourself isn't going to make you a more marketable or productive software engineer.

##### Share on other sites
Quote:
Original post by RDragon1
Quote:
 Original post by GanonPhantomWell, the project im working on, needs to stick to the current standard, and im not really "huncky dory" about boost (because of the "sticking to the standard" thing). So i guess for now im stuck with repeating overloaded functions.

Boost is implemented using standard C++.

All you're doing by writing your own solution is reinventing what already exists in boost, except it will be less generic, and all-around less useful, not to mention it's more code you need to maintain and debug.

Avoiding educating yourself isn't going to make you a more marketable or productive software engineer.

There is a difference between IMPLEMENTED with the standard, and IS the standard.

I have nothing against boost, but i cant use it for my project. (not my call)

I find it quite childish to tell me that im "Avoiding Education" because i dont use boost... this is honestly one of the biggest reasons why alot of people stay away from GameDev. Because the first word out of anyone's mouth is "BOOST!!!!!", followed by a formal reply by the OP saying that he/she dosnt want to use boost for a reason or another. Thereafter he/she will get several other posts that are unkind and irrational just because he/she will refuse to use boost.

Boost has to be sold believe it or not. People arnt just going to pick it up and use it in their projects just because its Boost.

We are not going to make people download and setup boost just to use our source code when its released.

Once again, nothing against boost personally... but that is awfully childish of you, as anyone would agree you get smarter by doing something yourself, as opposed to have something do it for you.

As for the "reinventing the wheel", everything that is in boost has existed one time or another somewhere else, and at that, modern civilization would still be in the stone age if we didnt "reinvent the wheel" (stone wheel anyone?). Think about things logically.

Now, im not saying my implementation is going to be better then boost, nor am I trying to create my own boost (which seems to be the assumption in every boost hater post), but honestly, let people use what libraries they want and provide actual help like others above you did. Yes, they did provide boost solutions, but the difference is that they did not come back and try to tell me im "avoiding education" because im not using boost after i said NO to boost.

GanonPhantom

##### Share on other sites
Your last post appeared to imply that Boost wasn't standards-compilant, when in fact it is. It's fine if your higher-ups have sent down the command "don't use Boost", but if your reasons for not using Boost appear to be incorrect, wouldn't you expect people to respond along those lines? Honestly, the anger in your last message is unprofessional and utterly uncalled for. If you're trying to be doing something that can be easily accomplished by the use of a common add-on library, people are going to suggest that add-on library. If your stated reasons for not using a particular tool appear to be based on incorrect assumptions, people are going to correct you on those assumptions.

Now, how about an apology for RDragon, and perhaps a thanks to him as well?

##### Share on other sites
Quote:
 Original post by SneftelYour last post appeared to imply that Boost wasn't standards-compilant, when in fact it is. It's fine if your higher-ups have sent down the command "don't use Boost", but if your reasons for not using Boost appear to be incorrect, wouldn't you expect people to respond along those lines? Honestly, the anger in your last message is unprofessional and utterly uncalled for. If you're trying to be doing something that can be easily accomplished by the use of a common add-on library, people are going to suggest that add-on library. If your stated reasons for not using a particular tool appear to be based on incorrect assumptions, people are going to correct you on those assumptions.Now, how about an apology for RDragon, and perhaps a thanks to him as well?

The assumption is infact incorrect, and it is partly my fault on the wording. What was ment to be implied is, that we could not use it for the fact that we do not want to require our uses to use libraries that are not standard with every compiler, such as the GCC and MS Visual C++. Basically, we want it to be plug-in-play.

I do apologize to RDragon, but i think it was awfully childish for him to say im "Avoiding Education" by not using boost. I was well aware that boost used the standards, and as i have said above, i have nothing against it.

##### Share on other sites
Quote:
 Original post by GanonPhantomBecause the first word out of anyone's mouth is "BOOST!!!!!", followed by a formal reply by the OP saying that he/she dosnt want to use boost for a reason or another. Thereafter he/she will get several other posts that are unkind and irrational just because he/she will refuse to use boost.

Nothing indicated you cannot use boost. You simply stated that you're facing a common problem in C++, to which you were given the obvious solution. Even more, your arguing implied that you are not familiar with boost.

This results in something like this:
A: "I'm trying to shape a stone as to maximize its hammering efficency"
B: "Use hammer"
A: "I prefer stones since they're easier to find"
B: "???"
A: "Oh, we work in highly magnetic environment and cannot use metallic objects".

The reason everyone says boost is because the following is what usually happens:
- Someone is trying to do something they shouldn't be doing
- Someone is re-inventing the wheel for the n-th time in a situation where that really isn't needed
- Because boost is effectively standard library extension and is damn useful
- Just looking at how boost does it is often enough to establish what the generic case is, and is usually trivial to duplicate trivial version of that.
- There's a million ways to do something that boost does differently, and that is typically stated as core requirement ("boost::function uses heap allocation and double indirection resulting in 85% overhead in release testing")

The biggest problem is always "needs to excel at communication and team work".

As an example:
"Hi, i'm working a problem yadda yadda. I'm required to implement blah blah. The project we're working on is to be shared between C and C++ environment using foo and baz compilers. Boost has not been accepted by management, libXYZ conflicts with our implementation etc., and whatever has proven to generate buggy code under bar compiler. Are there any other options to meet the requirements I have".

And, since this is for beginners forum, the obvious solution will always be the most standard and straight-forward one.

Quote:
 we could not use it for the fact that we do not want to require our uses to use libraries that are not standard with every compiler, such as the GCC and MS Visual C++.

This is incorrect statement. At least look at supported compilers, boost will cover much more than that in a reliable, peer-tested and portable manner. Unless your team can afford to support several *dozen* compilers on dozen platforms, which boost does out-of-box.

1. 1
Rutin
19
2. 2
3. 3
JoeJ
15
4. 4
5. 5

• 22
• 19
• 11
• 13
• 17
• ### Forum Statistics

• Total Topics
631698
• Total Posts
3001768
×