Hacker News new | past | comments | ask | show | jobs | submit login

> The very fact that templates are not classes, specifically not base classes for all derived variants of the template is perplexing

I'm a little confused at what you're trying to get at here. What do you want to do that needs such a thing? What is the equivalent Java/C# code you have in mind?

> but it limits the usefulness of templates past mere containers.

What kinds of uses did you have in mind for a "template base class"?




Extend a template and specialize it for some type. Extend the template again and specialize it for some other type.

The two resulting derived classes are not the same, and you cannot have a function that accepts the base template with a wild card type and pass both types into it.

In Java, you can do: void myFunc(List<?> list); and all List objects with any type can be accepted by this method, in addition any class that extended List can also be accepted into this method, such as ArrayList<?> or whatever.

As far as I've seen, this is impossible in C++, unfortunately.


Why not

    template<typename T>
    void my_func(std::vector<T>& vec);
?

The reference (or use a pointer, if you want) ensures that derived types can be passed, and the template allows for any parameterization to be used.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: