r/cpp • • Mar 30 '15

Simple Question

So I was thinking the other day, and I came up with an idea that I decided to try... a function returning an anonymous struct!

I tried it, and lo and behold, it doesn't compile. A closer look reveals that the Standard specifically disallows defining new types as the return type of a function definition! Though it does appear that I can return an anonymous struct using decltype, that is even more code than simply defining a new type name.

My Simple Question is... why? What reason did the standards committee have to explicitly disallow this? Is this a case of them trying to save users from their own stupidity, or is this technically infeasible somehow?

1 Upvotes

20 comments sorted by

View all comments

Show parent comments

3

u/acwsupremacy Mar 30 '15 edited Mar 30 '15

Because it would be a simple, immediate way to have a multivalued return. You wouldn't necessarily need to even store it; if all you need is one value, you could use it immediately:

int x = multifn().integer;

If you wanted to store it, well, auto can handle anonymous types just fine.

At the end of the day, I just don't buy the "why would you want to do this" argument. For anything. If it is not difficult to implement, it would not twist the grammar, it would not break binary compatibility, and it wouldn't subvert the type system, then who cares whether it would be useful or not? I strongly suspect disallowing it actually adds complexity to the language definition, since as far as I'm aware the existing grammar absolutely includes constructs like this, and they must be tested for and explicitly diagnosed by compilers; but of course having no understanding of the nitty-gritty details of compiling, it is absolutely possible that some lurking problem might arise if they tried to do this. That's why I'm asking.

2

u/coachkler Mar 30 '15

While not totally anonymous, you can do this with a tuple. What's the benefit of an anonymous struct as a return vs. a tuple?

3

u/[deleted] Mar 30 '15

Meaningful names for the items.

2

u/STL MSVC STL Dev Mar 31 '15

get<MyType>(mytuple) is maybe 75% as good as having names, and certainly a lot nicer than indices.

1

u/[deleted] Mar 31 '15

That would be okay for tuples in which the elements have distinct types.

But they maybe don't, so IIRC C++ (for example, and as you presumably know, judging from your name!) has a get that takes a zero-based constexpr integer.

Still it would be a fun exercise in template metaprogramming to define a variant of get that did accept a type, and for extra credit produced an intelligible compiler diagnostic if the tuple has more than one element of the requested type. feels nostalgic for brief dalliance with boost hacking about 10 years ago...

2

u/STL MSVC STL Dev Apr 01 '15

It's not hypothetical - get<T> was voted into C++14 and has been implemented in VC 2015. It's well-formed when T is unique (there can be duplicates elsewhere) and ill-formed if T is duplicated (implementations will typically emit nice static_asserts).

2

u/[deleted] Apr 01 '15

Nice!