3 ms·
> who dislike inference in languages where it has them and prefer explicit typing out types So you prefer std::vector<some_template<some_foo>> foo = new s
by viraptor 7y ago
> who dislike inference in languages where it has them and prefer explicit typing out types
So you prefer
std::vector<some_template<some_foo>> foo = new std::vector<some_template<some_foo>>();
Rather than replace the first one with var / let / auto / whatever your language does? Why? The type is already there.
- Crinus 7y agoIn terms of "liking" it, no, in that very specific case where the type is repeated and very long, i do not like it and i think "auto" (or var or let or whatever) looks better. However in the interest of consistency (and to a lesser extent, avoiding 'slippery slopes') i'd rather type that than use auto. And even then that this is only in the very specific case you showed, where the type is too long, it is repeated and the constructor parameters - if any - are visually dwarfed by the duplicated type. In practice these aren't as common as other uses of types like loop iterators for member/obtained collections (where the use of 'auto' makes it impossible to know the collection type unless you check the collection declaration) or local variables you assign to the result of some function (the return type of which again you need to check the function declaration to see) and - at least in C++ - you rarely need to do "very_long_foo* bar = new very_long_foo" as a stack allocated "very_long_foo bar" is enough (so no need to double type the type).
- wrongexample 7y agoMaybe you're getting mixed up with Java, but that example type is wrong, since you used `new`, the vector was allocated in the heap and the appropriate type is a pointer type: `vector<T>*`. What's more, that's a highly unusual example. Rarely would you need to heap allocate a vector. In the case of stack allocation the syntax would be `vector<T> myVec` which is as succinct as possible.