English 中文(简体)
Template Classes in C++ ... a required skill set?
原标题:

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

问题回答

You ll need them sooner or later ;) But for a beginner they are a bit tricky to write (but easy to use, so use STL intensively). And after mastering other c++ skills (const-correctness, operators overloading, learning about compilation units etc) and gaining some experience you will start coding templates for yourself. So my advice: use them but learn how to write them when you ll find you need them. It will be much quicker and effective learning if you gain some experience with standard c++ constructs first.

At my employer, the main thing we use that s in C++ and not in C is indeed generics, AKA "templates". Inheritance &c are nice (but with pluses and minuses), but templates are absolutely crucial -- the main, cost-less advantage of C++ over plain C. Master them, you won t be wasting time!-)

Of course they are used in the industry. Just look at the Standard Template Library (STL)!

You definitely should know how to work with templates, and better, learn how to do some fancy things with them like template metaprogramming, ok, that may be optional, but template classes are not!

EDIT: Even more, a lot of other languages like Java and C# support the concept of generics, that being the equivalent to templates in c++, but generics are far more restrictive.

In addition to what the others said, they can be incredibly useful in your own code, and you definitely will come across it while reading others , so there is no harm in learning it.

You cant go far without mastering templates in C++ if your writing code on windows. ATL, STL and various other windows libraries heavily use templates.

Side note*. If your using visual studio , you might want to learn the quirks which are used in visual studio to support templates. For example , many windows libraries often declare their entire contents in header files to work around linker bugs. Its annoying, but this limitation often is a stumbling block for newbie s learning C++ using visual studio.

No, don t spend to much time on learning how to implement them. The other answers correctly explain that you certainly should learn how to use them. That makes sense; many libraries provide class templates and you should be learn to use those libraries. But writing your own templates is much rarer.

I d say understanding templates is a requirement for modern C++ style and idioms and getting the most out of the language. They aren t as difficult as one is often made to believe. If you are a beginner I d absolutely recommend a book like Accelerated C++ which offers a very gentle and natural introduction to templates.

As others have noted, Templates are a very important part of the C++ language. In fact, they re a turing-complete language themselves ;) However, not only does the STL make heavy use of templates (ever used std::vector or std::pair?), lots of other libraries do, too... the most notable is probably boost. I hope that gives you an impression on how important they are.

What you should definitely learn is how to use them in the context of classes and functions. When you mastered that, you can move on to more advanced topics like template specialization or template-template parameters. And if you still haven t got enough of templates, you can check out "C++ Templates: The Complete Guide" by Nicolai M. Josuttis and "Modern C++ Design" by Andrei Alexandrescu. Even though the latter is really a hard piece to understand.

For a start, this resource http://www.parashift.com/c++-faq-lite/templates.html might be helpful, too :)

Please also note that Generics aren t "Templates in C#/Java" or vice versa... they follow a different concept.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签