|
I am using the STL all the time. The clean structure of its containers and algorithms combined with your own defined function objects makes it an indispensable tool. The structure of your code becomes cleaner, more readable and compact. When you first understand the concept behind it you are able to do things not possible before. The fact that other languages do not have the same mechanism combining clarity with effectiveness is good enough reason to stick to C++.
Vidar Gunstvedt
|
|
|
|
|
Now then I'm not as alone as I think I'm. Templates are fascinating and with ATL/STL/WTL(once you get to know what's under the carpet), I first came to know the sheer power of templates. Yes! I'm also a recovering ATL programmer.;)
#define MOSTLY_LEAN_AND_MEAN
|
|
|
|
|
Recovering?
BAH, ATL/WTL is a disease which I always want to be inflicted!!!!
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
ALT is great, really great
- Anders
Money talks, but all mine ever says is "Goodbye!"
|
|
|
|
|
Anders Molin wrote:
ALT is great, really great
ALT is hard to live without. And CTRL and Shift too for that matter.
Sonorked as well: 100.13197 jorgen
|
|
|
|
|
Jörgen Sigvardsson wrote:
ALT is hard to live without. And CTRL and Shift too for that matter.
Don't forget ÿ as well. :P
* Should be the Windows Logo *
honk() if $you->love(PERL);
|
|
|
|
|
Bruce Duncan wrote:
Don't forget ÿ as well.
What the?
Sonorked as well: 100.13197 jorgen
|
|
|
|
|
|
Ah! but you do not know how deadly this template addiction is!!. While browsing thru Mike B's Pro MFC programming, I was amazed to see the MFC ActiveX control wizard. I have NEVER seen that wizard page, not once! And now with .NET,(esp. as not much improvements are being planned for ATL), I'd be sooo brokenhearted to see it go.
Alas! Elvis is dead.
#define MOSTLY_LEAN_AND_MEAN
|
|
|
|
|
I'd heard the figure was quite large (in fact, much larger than 20%) so I thought it would be interesting to get some stats from CP readers.
Kinda kewl.
cheers,
Chris Maunder
|
|
|
|
|
|
As soon as they add generics to the .NET framework (and thence C#) I'm gonna be a happy camper
cheers,
Chris Maunder
|
|
|
|
|
I asked a question about this in regard to container classes to a M$ guy and he never answered me, can you ? I want to know if the container classes in C# really are as ugly as I have heard ( the C# equivelant of a container of void *), and if they will be deprecated in favour of something that uses the generics they are in theory on the verge of providing ?
I've not had anywhere near as much C# time as I would like this week, I've been very busy at work, and at home I've been ripping 16 hours of 1980's Countdown. Maybe this weekend I'll finally get something going...
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
|
|
|
|
|
I guess you're talking about the ArrayList class in .NET. Yeah - it takes Object s so it is kind of generic (essentially a like CArray in MFC holding CObject* 's). Once generics are in .NET then I'd imagine the situation will be as we have it in MFC: use the standard version or use the strongly typed templated version, depending on your situation.
1980's Countdown? Oh man...
cheers,
Chris Maunder
|
|
|
|
|
Chris Maunder wrote:
essentially a like CArray in MFC holding CObject*'s
Actually, it's more like CObArray IMHO.
I vote pro drink
|
|
|
|
|
Yep - you're right.
Brain is on standby today
cheers,
Chris Maunder
|
|
|
|
|
So you don't know if we are going to be provided with some sort of templated classes at the same time as template support ?
Chris Maunder wrote:
1980's Countdown? Oh man...
I thought that would get a response
My favourite is the Countdown Awards 1981 when Cold Chisel trash the stage, and again in 1983 when Billy Idol appears totally wasted and off his head.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
|
|
|
|
|
range limit a value
template <class T> inline void clamp(T &val, const T & minval, const T & maxval)
{
val = max(val, minval);
val = min(val, maxval)
}
use: clamp(bob, 100, 200);
reset a value when the variable goes out of scope
template <class T> class varResetter
{
public:
varResetter (T &var) : m_var(var)
{
m_resetVal = var;
}
~varResetter()
{
m_var = m_resetVal;
}
private:
T m_resetVal;
T& m_var;
};
use that one like this:
int bob = 0;
printf("bob = %d \n", bob);
{
varResetter <int>bobReset(bob);
bob = 100;
printf("bob = %d \n", bob);
}
printf("bob = %d \n", bob);
outputs:
bob = 0
bob = 100
bob = 0
that one is handy when you have to temporarily change a member variable for the scope of a single function call, and that function has many ways out (too many returns, or the possibility of an exception, etc). true, it's sneaky, and can surprise people not expecting it. but it does clean up the code.
-c
Smaller Animals Software, Inc.
You're the icing - on the cake - on the table - at my wake. Modest Mouse
|
|
|
|
|
Its the template king.
Really like the the Resetter.
Give us some more!!
Giles
|
|
|
|
|
I have an article here with a class that works similar to your varResetter. Mine is a bit more sophisticated in that it's just a regular class with a template member function -- no need to pass it the variable's type. However, it comes at the expense of having to allocate/free memory from the heap, which can get expensive if done too repeatedly.
Regards,
Alvaro
Behind a beautiful woman there's usually a guy who just couldn't wait to get rid of her.
|
|
|
|
|
|
This one is one of my all time favourites. It's a universal VARIANT if you wish. Really cool indeed.
Other favourites are high order functors such as compose and function.
Other really nice tools when dealing with templates are concept_check and static_assert.
And if std::pair ain't good enough for you, then tuple is a nice tool to have in the toolbox.
Sonorked as well: 100.13197 jorgen
|
|
|
|
|
Well, in fact, anything that is templated and is reusable is cool with me.
Sonorked as well: 100.13197 jorgen
|
|
|
|
|
The most fascinating facet of templates (to me) is not their intrinsic raw power, but the unexpected ways how they intertwine with other C++ features like inheritance hierarchies to create constructs that resemble magic at first sight. Examples that come to my mind:- Automatic adaptor classes, as used in
loki::Functor ,
- compile-time numerics,
- compile-time metaprogramming,
- compile-time "virtual" function call, as implemented in ATL,
- metatypes (multitype variables, type lists).
And the list goes on
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Joaquín M López Muñoz wrote:
compile-time "virtual" function call, as implemented in ATL,
Anyone who really understands whats going on down in the guts of ATL should have gone into Quantum Mechanics rather than programming!
"There's a slew of slip 'twixt cup and lip"
|
|
|
|