3 ms·
I do not know Java very well, but from the article I got the impression that in Java you could use List<?> as a reference in non-generic code to a List of arbit
by adament 5y ago
I do not know Java very well, but from the article I got the impression that in Java you could use List<?> as a reference in non-generic code to a List of arbitrary type. I.e. you could assign:
List<?> variable = new ArrayList<Integer>();
That is not possible with std::vector<std::any>> in C++ without converting and allocating a new vector. I am not familiar enough with the C++ standard to say it definitively but similar rules apply for vectors of pointers see https://godbolt.org/z/no3r67P5z https://godbolt.org/z/no3r67P5z
- pjmlp 5y agoYou are missing the part that in Java generics (at least into Valhalla) are a type system feature. What the JVM ends up seeing is something like Java 1.4 bytecode would look like, and there is no need for allocations, because it is only references and generics don't support primitive types. While C++ templates are monomorphic so a few more tweeks are indeed needed all the way down to machine code. The principle of cleaning the underlying type applies though.
- deleted 5y ago[deleted]