3 ms·
The only case I know of are lambda expressions. And for those I would argue that the type is obvious, but impossible to name.
by contravariant 9y ago
The only case I know of are lambda expressions. And for those I would argue that the type is obvious, but impossible to name.
- roel_v 9y agoIn template meta-programming, there are many cases where you don't know types until your template is instantiated; which, when you're writing libraries, could be with many types. Auto is a 'nice to have' when doing regular OO or generic programming in C++, but it changes (some) template meta programming from 'too difficult to bother with' to 'feasible'.
- contravariant 9y agoI can think of cases where the names get so complicated you really don't want to bother writing them out in full, but I'm having trouble coming up with a scenario where you'd end up with anonymous type through templates.
- Sharlin 9y agoThe type, albeit nameable, may not be a part of the public API—it could be an implementation detail subject to change. This is the case with some standard library types such as those returned by std::bind where the exact name of the type is explicitly left unspecified. It is possible to enforce this by returning a type whose name has private visibility—such a type is perfectly usable from the outside of its declaring class except that you cannot refer to it by its name.
- mattnewport 9y agoFunctions can return local types whose name is then not accessible to other code. Other code can use that type via auto though. As mentioned in another comment, not being able to name or even easily refer to a type without auto is very common in generic code.