C++ template is clumsy

After knowing that C++ template is actually marco, feel kind of disappointed.

I implemented a generic double linked list to feel what it is like in C++, I have done that in Java.

C++ code is redundant, wherever your class or methods have something to do with the template, need template <class name>. And also C++ template is good for container class only. Your template methods should not touch anything inside your generic part, because this may get you into runtime error.

For example, you have a generic method that use generic class T, and then try to call T->print(), if T has define print(), everything is just fine, otherwise you will get runtime error. Whereas Java have interface to restrict the signature of a generic class. C++ may use inheritance to over come this but it won’t be type safe.

Not to mention C++ multiple inheritance may lead to diamond problem. That is exactly why I learned C, C#, Java before C++, because in my opinion C++ is difficult to use and clumsy. But even so, you will eventually need to use C++ someday if you are not obsessed to a paticular language. But learning C++ is still very funny, it will be easier if you have good memory, but sadly my memory is not so good.

Advertisements

4 thoughts on “C++ template is clumsy

  1. I have to object to some things you mentioned.

    Macros are just “stupid” text replacements. Templates are type-safe and very powerful — not only for container classes. The “container of T” idea was just the beginning of generic programming.

    You are right that there’s no type system for template parameters (yet!). So, it is a bit like duck typing. If a T walks like a duck, swims like a duck and quacks like a duck … it is a duck. Still, your template code is type-checked at compile-time and not at run-time. That’s the beauty of it. With generic programming via templates you’ll get high-level abstractions without runtime or space overhead.

    All that Java has is dynamic polymorphism. At least you’re not forced anymore to perform dynamic casts yourself. With “Java Generics” the compiler will do that automatically for you but it’s still not for free. I’m not saying that dynamic polymorphism has no use. I just say that in many cases we don’t need it and that better techniques can be used.

    I also came from Java and I have to admit It took some time getting used to C++. First thing I did was to design some bloated class hierachy with virtual functions because that’s the way I did it in Java and the only kind of abstraction I knew to make code reusable. C++ enables you to write in a totally different style. You should explore that and get comfy with the common practices & idioms. There are many roads that lead to Rome. You might not have found the good ones yet.

    – P

  2. I know too little about C++ and not to mention it’s compiler.
    So, I didn’t think of dynamic linking and overhead, etc.
    Just a feeling of a newbie, should be so very wrong =P.

    I need sometime to digest what you have written. It is too difficult for me.
    Thank you for teaching me so much. I am getting used to C++ and I think I will find it very useful. But not that easy to understand as Java and C#. I know Java overhead and it’s performance are Java’s weakness and is the trade off to its friendly syntax. C++ idioms is complex for sure.

    but I don’t understand what you mean by type safe.

  3. I think you are confused about run-time and compile-time.

    “For example, you have a generic method that use generic class T, and then try to call T->print(), if T has define print(), everything is just fine, otherwise you will get runtime error. ”

    If T->print() is not defined, you get a compile-time error. So this error is caught before the code is ever being run. Compile-time error catching is far more powerful than run-time.

    “Not to mention C++ multiple inheritance may lead to diamond problem. That is exactly why I learned C, C#, Java before C++, because in my opinion C++ is difficult to use and clumsy.”

    Using multiple inheritance is your own choice, not a requirement. You may or may not use it. So I find it strange that you see C++’s multiple inheritance support to be a bad thing.

    … Alan

    • Thank you for correcting me. I am a noob to C++ and those are my ignorance comments. now I read them and feel stupid. lol
      But I wonder if C++ have anything like C# “where” to govern generic classes?

      I think the diamond problem can be avoid with good documentation.
      C++ is really funny and useful to code game. But my job don’t need C++. I really want to have more time to dig into it.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s