Sign in to follow this  

Templates

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I suppose you don't ever really need to write your own templatized functions or classes. Templates are just a tool; sometimes they are the right tool for what you want to do, but there are almost always other workarounds.

However, I would suggest getting comfortable with them, since they can be very helpful. Is there some particular aspect of templates that you have trouble with?

Share this post


Link to post
Share on other sites
Somewhat ironically, I didn't start using templates properly until I started using dynamically typed languages, such as Python.

In such languages, the runtime doesn't care what the type of the object is, or even whether it's part of a certain hierarchy, as long as it supports the required interface ("duck typing"). A common example in Python is that you can often treat an object that has a write() method as it were file.

C++ templates allow you to do the same thing, but the checking is done at compile time rather than at runtime.

Of course, you can get in to all the meta-programming stuff if you really want to. There's an entire functional language to be discovered in there, but you don't need it for starters. I'd just make sure you keep using the ones from the standard library (and boost) and make sure you're comfortable using them.

If you're anything like me, you'll be coding away and it'll suddenly hits you that you need to use templates to do something. The smoke will clear and a whole new world of wonderous nuttiness will present itself before you.

Edd

Share this post


Link to post
Share on other sites
Nothing is "necessary". If you don't feel like using functions, string or integers then you can choose not to, but you don't take full advantage of the tools provided to you. Templates is one of the very powerful tools in C++ which you better get comfortable with at some point. Even if you somehow manage to refrain from using templates yourself then you still have to read and possibly modify other peoples' template code.

If you are just starting out then you could postpone it a bit, but don't wait for too long, since it's a major part of any good C++ program and without it you may teach yourself some bad habits to get around the limitation you're imposing upon yourself by not using templates.

Maybe you should consider whether you are moving too fast. Learning to program isn't just about going through a book. You need to create some programs yourself if you want to get comfortable with the constructs offered to you. If you don't get comfortable with the basic language constructs before moving on then the amount of stuff which you don't find natural to work with might get overhelming and programming could suddenly become extremely hard.

Share this post


Link to post
Share on other sites
Entire games (and non-game applications, even operating systems) have been written without a single template. Therefore they are not necessary by any stretch of the imagination. But, they are often useful.

Share this post


Link to post
Share on other sites
Quote:
Original post by Driv3MeFar
I suppose you don't ever really need to write your own templatized functions or classes. Templates are just a tool; sometimes they are the right tool for what you want to do, but there are almost always other workarounds.

However, I would suggest getting comfortable with them, since they can be very helpful. Is there some particular aspect of templates that you have trouble with?


Well...

I really don't like the way they look, but that is not too major.

Usually I can visualize how some parts of code are going to interact but I just cannot see what they would do.

I might just need to get more familiar with templates.

I have not came close to using templates yet. Not even gotten to a point where I would need to decide on a workaround or to use templates.

Plus I have this thing, kinda like OCD, with doing things in some symmetrical way or the fastest way possible.

I hope that was comprehendible.

Share this post


Link to post
Share on other sites
Quote:
Original post by Shlaklava
Other than the STL in C++, is it really necessary to create my own templates? I just don't feel comfortable making them.
People don't write templates just because they look pretty; People write them because they want to use them for something!

If you don't have something you want to templates for, then by all means don't use templates.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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