Sign in to follow this  
GanonPhantom

"..." for templates?

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by GanonPhantom
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.


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 this post


Link to post
Share on other sites
Quote:
Original post by RDragon1
Quote:
Original post by GanonPhantom
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.


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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
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?


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 this post


Link to post
Share on other sites
Quote:
Original post by GanonPhantom

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.


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.

Share this post


Link to post
Share on other sites
Quote:
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.

Agreed, that was a bit over the line.

Now, the nice thing about Boost being written in C++ is, it's written in C++. That means that even if you Cannot Use Boost, you can still use bits and pieces of Boost simply through copying and pasting. Different boost components will give you varying levels of success at this: boost::signal, for instance, pulls in a LOT of other stuff, so that's not really feasible, whereas safe_deleter is, like, three lines of code.

boost::bind and boost::function, unfortunately, won't separate from the rest of Boost without a fight. Bits and pieces of boost::preprocessor will, but the file iteration stuff (which is how you'd want to solve this particular problem) involve hundreds of fiddly little files. So no help there either.

So that's the long version of saying "In this case, snipping off pieces of Boost won't help; but don't rule out using bits of it under other circumstances, even if you don't import it as a whole."

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus

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.


I think you have misinterpreted. The reason why we cant/dont want to, is we want it to be plug-in-play, as in, no extra non-standard libraries that dont already exist with GCC or VC++. We want you to be able to open up the solution/project, and be able to build there and now.

Share this post


Link to post
Share on other sites
Quote:
Original post by GanonPhantom
Quote:
Original post by Antheus
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.
I think you have misinterpreted. The reason why we cant/dont want to, is we want it to be plug-in-play, as in, no extra non-standard libraries that dont already exist with GCC or VC++. We want you to be able to open up the solution/project, and be able to build there and now.
I understand fully that you aren't going to use boost, but as a point of pedantry, there is nothing to stop you embedding some or all of boost in your own source tree, and using your own build system (this can be tricky to get right). I do this for a number of libraries, and it works pretty well, although some of the less common boost libraries (spirit and phoenix in particular) are a right pain to build without using boost-jam.

Share this post


Link to post
Share on other sites
Quote:
Original post by GanonPhantom
Quote:
Original post by Antheus

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.



I think you have misinterpreted. The reason why we cant/dont want to, is we want it to be plug-in-play, as in, no extra libraries that dont already exist with GCC or VC++. We want you to be able to open up the solution/project, and be able to build there and now.

Edit: it moved my post down when i tried to edit it 0.o, mabe i accidently pressed the quote button on myself :P

Share this post


Link to post
Share on other sites
I've run into this problem too, and I can feel your pain. Sadly, the most straight forward approach to handle this is straight up copy-paste-add a template argument. The Boost file iteration stuff works well for this (and iirc Luabind uses it) although it has the strange ability to really muck up readability and maintainability. You can easily argue that the copy-paste method causes just as many maintenance problems, since if you want to change something you now have to go through every iteration, although I do find it a bit easier to read through. There is no real 'clean' way to solve this problem yet in C++. Variadic templates can't come soon enough imo.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this