3 ms·
> Don't go crazy with Templates, in fact don't even use them (except for what's in STL already) What's wrong with templates? This is the most powerful feature
by kombine 14y ago
> Don't go crazy with Templates, in fact don't even use them (except for what's in STL already)
What's wrong with templates? This is the most powerful feature of C++ allowing for Generic programming, Polymorphism and metaprogramming. The latter is of course clunky considering C++ syntax, but overall templates are great. And STL is the best thing that happened to the C-like family of the languages.
- raverbashing 14y agoThe problem is that, as expected in a strongly typed language, you can't do real Generic programming, even with templates. Or even adding another inheritance level (which makes some things easier) Because type 'fixing' (that is, knowing what you need) happens at compile type, for example, duck typing ("even with" dynamic_cast - yes, dynamic_cast works at run-time, but you're limited in what kind of objects - by your inheritance hierarchy - can be present at the time you're doing the dynamic_cast) You end up having to specify the type, you don't have a 'Generic' type. And you may have to deal with dynamic_cast And read again, I'm not against STL, they 'get' templates. Edit: confusing explanation
- kombine 14y ago> You end up having to specify the type, you don't have a 'Generic' type. And you may have to deal with dynamic_cast Do you really need one? Because then you really are dealing with run time polymorphism, whereas templates are all about compile time. I recommend checking D programming language out. It has very powerful templates with a nicer syntax than C++ and a lot of other things aiding metaprogramming. Moreover with Ranges you get an implementation of ideas laid out in STL but with a better syntax and matching the power of what is in proper functional languages.
- nitrogen 14y agoQt is capable of lazy typing in C++ using QObject and QVariant, though it also adds its own meta-object compiler on top of the C++ compiler.
- apaprocki 14y agoWhich is really no different than using GObject and GValue from GLib / Gnome world. (Even using a meta-object GOB compiler :))
- nitrogen 14y ago... or using a union with an enum in plain C: struct data { void *data; size_t length; }; union value { char *zstr; int intgr; float flt; struct data *data; }; enum type { STRING, INTEGER, FLOATING, DATA, };