Hacker News new | ask | show | jobs
by germandiago 809 days ago
The type of a lambda cannot be spelled. The type of a local class return cannot be named from outside either.
1 comments

By "local class return", do you mean a function that returns what Java calls an "inner class"? That's perfectly describable (in C++ as well) if the inner class is public rather than private. It's unnameable, not because of the type, but because of the access (private).
You can create a class inside a function and return an object of it. That name is not legal outside of the function but its methods can be called.
Ah, I see. Inner to the function, not inner to the class. So the C++ syntax would need to be something like:

  ClassName::functionName::innerName
Except that's not an actual C++ syntax - there isn't one.

But if you return it from a function, how do you declare the variable you assign it to? auto?

And, is auto a valid return type from a function? If not, how do you declare the function that returns this object?

Exactly. You spell auto or an interface or base class whose derived class cannot be named. I think both are ok. In the latter case you can name the interface instead of auto.

Yes, auto is a perfectly valid return type, usually quite useful in generic code.

However, in generic code, starting at C++20, I would recommend to use concepts to name a return type that can be many different thing but that is compliant with the same "compile-time" interface.

You could do:

    forward_range auto f(auto && input);
You do not spell the return type but you are saying that type conforms to forward_range. Note that the auto type returned could be one of many unrelated types and it is computed at compile-time.