4 ms·
> but it limits the usefulness of templates past mere containers. Your post (specifically, your conclusion) makes me think that you are fundamentally mistaken
by dragontamer 6y ago
> but it limits the usefulness of templates past mere containers.
Your post (specifically, your conclusion) makes me think that you are fundamentally mistaken about how to use templates.
Template metaprogramming is an old-trick at this point, with a huge variety of applications in C++. But even if you ignore Template metaprogramming (which many argue is too difficult to understand...), bog-standard C++ techniques like CRTP (Curiously Recurring Template Pattern) shows how useful templates can be outside of containers.
https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern https://en.wikipedia.org/wiki/Curiously_recurring_template_p...
---------
C++ templates are extremely different beasts than generics. Templates are closer to a meta-language than a language feature. They hold important roles in optimization... specifically allowing you to convert "runtime code" into "compile time code" in a huge class of problems.
CRTP is a great example of retaining OOP-style polymorphism while compiling down into highly efficient non-virtual calls... with practical applications into GUI frameworks, video games and more. And its only made possible due to templates.
- Alupis 6y agoInteresting, I've never heard of CRTP and will need to read up on it. I do "get it" that templates are not generics... there's just a lot of gotchas for someone assuming generics, err - templates are similar to other languages. Like the difficulty in nesting templates in templates...
- dragontamer 6y agoEven in the use of "containers", generics are the complete opposite of how to use templates. Java / C#'s concept of "Everything derives from Object" is outright rejected in C++. Templates are part of this culture: how can we provide extendibility when nothing is an Object? Well, look at iterators in C++. All C++ iterators extend from all collections WITHOUT the use of OOP-derivations. That's thanks to the magic of Templates. ------ What's going on? C++ templates allow the creation of new iterator classes. It invokes the compile-time language to create a new class at compile time. In Java / C#: vector<MyClass> is the same old vector as all other vectors. In C++: vector<MyClass> is a custom-made, completely separate class from all other vectors. vector<MyClass>::iterator is a 2nd, custom-made completely separate class from all other iterators. And yet, all of these different classes can be used the same, thanks to the use of auto, or other compile-time structures in C++.
- Alupis 6y agoGetting Java's generics out of your head when doing C++ is definitely a challenge. It's just so disappointing you cannot do something like: void myFunc(List<? extends MyDataContainer<?>> list); Instead you end up with a base class MyDataContiner and then manually extending it with something like MyDataContinerInt or MyDataContinerLong etc... C++ templates may be very powerful, but limitations like this feel out of place in a language as bloated as C++ already is... Admittedly, I still have much to learn about C++. It's been quite an experience, frustrating at times, learning this language.
- aw1621107 6y agoI think you can get something similar now with C++20 concepts? template<typename T, typename U> void my_func(std::vector<T>* vec) requires std::derived_from<T, my_data_container<U>>; (Probably mangled something, but the idea is there) Without concepts, you're left with SINFAE: template<typename T, typename U, typename = std::enable_if_t<std::is_base_of_v<my_data_container<U>, T>>> void my_func(std::vector<T>* vec); or just a regular function and a hope that anything wrong will be caught by the compiler: template<typename T, typename U> void my_func(std::vector<T>* vec) { // use my_data_container<U> somewhere and hope things blow up if something is wrong // Not 100% sure this is allowed, but something similar should be at least }
- dragontamer 6y agoI'm curious: what's wrong with myFunc(List<BaseClass * >) ?? EDIT: Remember, in C++, "BaseClass" cannot polymorph, only "BaseClass * " can. (BaseClass is where the data is stored precisely. If its 20 bytes of RAM, then DerivedClass might be 24 bytes of RAM, and therefore can't fit. But both BaseClass * and DerivedClass * are compatible with each other, and may convert into each other, as long as you know the pointer goes to the right kind of class) And if that's not sufficient, then a static_assert over the type information is probably what you need. Ex: template<class T> void myFunc(List<T> list){ static_assert(std::is_nothrow_convertible(T, BaseClass)); // I haven't tried, but this probably works } That's what I mean about C++ templates: they're a meta-language. You can add if-statements / else statements over type information at compile-time in C++. In this case, we've created a static_assert (aka: a compile-time error will pop up) if T does not convert into BaseClass (note: T may have a CopyConstructor(BaseClass), so maybe its not exactly tthe same concept as what you're trying to do... but this gives you an idea of C++isms...)
- ubadair 6y agoTemplates are the killer feature of C++. They add incredible power to the language, but also dramatically increase its complexity. Templates are only superficially similar to generics in C#/Java. They are essentially a type-safe, Turing-complete, syntactically-constrained macro system deeply embedded into the grammar of the language. Few other programming languages offer a similar feature. C++ pulled it off with an ISO standard and multiple conformant implementations. Templates are loved by many, but they are a contender (along with UB) for the most hated C++ feature due to complexity.