开发者

Learning C++ using a template

Do you recommend learning C++ using a template?

Edit

I have basic programming language (I know开发者_Python百科 how to work with things like loops, arrays, classes and functions) and I have been recommended to follow tutorials in a template that is already made for creating games.

I would like to become a game programmer.


No. C++ template is the last that newbies should touch. First learn the basics: loops, conditional blocks, various syntax, then class/struct. And then templates! However, you should learn to use STL, almost from the beginning. Please note that using templates (such as STL) is one thing, and writing your own class/function templates is another. First learn to write non-template class and function. Once you're confident, only then start learning and writing your own class and function templates!

Buy any introductory book, as listed here, and start reading it:

The Definitive C++ Book Guide and List


The short answer to your question is yes.

The longer answer is yes, BUT you need to consider when to move into the C++ template world. C++ is a very rich languare and comes with enormous power and possibilities. To use this power one needs responsibility and one must have the knowledge to use things wisely. There are many things one needs to understand in C++ in order to write good code and templates is not in the list of most important things.

However, as already mentioned STL (Standard Template Library) is somewhat a central piece in C++ and is, as you see from the name, a library using templates (heavily). In my opinion, STL is something you should start looking at when you start learning C++. For this you will need to understand basic concepts about templates. The same goes for boost, which is a masterpiece in C++ template programming. Here you might need even more understanding what templates are, what they look like and what they do.

My advice is to start by learning the C++ fundamentals without involving templates. This is by itself a big domain and will take a long time to master. When you feel comfortable here, take a look at STL and maybe later boost. Here you can get familiar with template programming as a user of templates in your code. Again, these libraries are big and require work to understand properly so you can use them wisely in your own code.

Here is a nice article about programming in general and what it takes to learn something. C++ is not an exception and requires a lot of time and work.

My own experience in C++ is soon 10 years and I still have a lot to learn, especially about templates. I use them daily, but always try to see when they actually make sence to use. I'm not now talking about using STL or boost, because this I believe is part of my C++ base toolkit. I'm talking about writing my own templated code. What I have seen where I've worked is that people many times have problems with templates and making good software. In my opinion, only a master knows when to be in the template world, when not to and how to combine the two.

Still, templates are very useful and I believe all C++ programmers should have some understanding of them. That level of understanding depends on their own usage of templates and over time you will yourself decide what you use from C++'s huge toolkit.

C++ templates - The Complete Guide is considered the book about templates and is definitely worth a read (or many;)).


Yes, you should learn the template aspects of C++ programming, since the majority of the standard is based on templates. For instance, you are much better off learning about arrays via the std::vector<T> class template or possibly the more recent std::tr1::array<T>, than using raw C-style arrays.

Note that what I am recommending is that you use template-based libraries that have been provided for you. You most definitely should not start learning by trying to write template code yourself, which requires a fair bit of background knowledge to get right. In particular, writing exception-safe template classes is a tricky process that took the C++ community years to get their collective heads around.


Templates gives the powerful feature of generic programming to C++. So, you should not neglect it.


The basics of templates is very simple: just writing code where one or more of the types or values is initially left unspecified, but will be explicitly specified sometimes before compilation. It's very easy to write and understand simple uses of templates, so don't let yourself be scared off.

For example of how simple it can be to get something genuinely useful, say you have two structures representing incoming network messages A and B and they contain a significant set of same-named fields, even though the exact types and position in the structures vary, you might write reusable code to extract those fields into some internal structure I as in:

struct X { int k : 5; char m[2]; float l; bool only_in_x; };
struct Y { int k; double l; char m[4]; };

struct I { int k; double l; char m[4]; size_t m_len; bool only_from_x; };

template <class T>
void load_common_fields(I& i, const T& t)
{
    i.k = t.k;
    i.l = t.l;
    memcpy(i.m, t.m, t.m_len = sizeof t.m);
}

In the code handling incoming messages, you can then do something like...

switch (network_message.message_type)
{
    case X:
    {
        X* p_x = (X*)buffer;
        load_common_fields(i, x);
        i.only_from_x = p_x.only_in_x;
        break;
    }
    case Y:
    {
        Y* p_y = (Y*)buffer;
        load_common_fields(i, y);
        break;
    }
}

This is much simpler and faster than using run-time polymorphism, with virtual functions and common base classes. Why not use it? It's less ugly but effectively equivalent to - and hopefully easy understood by comparison to - using a preprocessor macro to generate functions for each type involved ala:

#define LOAD_COMMON_FIELDS(T) \
    void load_common_fields(I& i, const T& t) \
    { \
         ...

When you're very comfortable with such simple uses start playing with SFINAE, CRTP, meta-progamming, policy-based design etc. as you naturally observe repeating issues with your code and get that "oh, I can see how that's useful" feeling as you read about them. If you read about something in your programming studies and you can't see practical ways it could have helped in your recent projects, then you may not be ready for it yet.


No, It should be last. Learn the basic functionality first. I have given link. you can learn from it.

http://www.cplusplus.com/doc/tutorial/


Do you mean the template mechanism of C++? Yes you should definitely learn this feature, because it is one of the key concepts of C++. You will get in touch with the so called "Standard Template Library (STL)" very soon, for example when you want to handle a vector of data.

See the following references for an introduction:

  • http://en.wikipedia.org/wiki/Standard_Template_Library
  • http://www.sgi.com/tech/stl/

You should at least understand the concept of the template mechanism in order to understand the basic container and algorithm templates of the STL.

Nevertheless, you should learn the C++ language itself first.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜