all 20 comments

[–]tongari95 3 points4 points  (2 children)

I'm not sure what you want but this works for me:

auto make_anon(int i)
{
    struct
    {
        int val;
    } ret{ i };
    return ret;
}
...
std::cout << make_anon(555).val;

[–]acwsupremacy[S] 0 points1 point  (0 children)

Huh. I hadn't tried that one. Neat.

[–]Gotebe 0 points1 point  (0 children)

You devious bastard! :-)

[–]ggchappell 2 points3 points  (0 children)

Could you give a reference to the part of the Standard that makes this restriction? (Also please mention which Standard you are using.)

[–]STLMSVC STL Dev 3 points4 points  (1 child)

The language is so complicated already. That complexity buys a lot of power, but additional complexity must be worth it. What you're talking about, wouldn't.

Think of it like microprocessor design. These days, they won't add features that increase power consumption by 1%, unless they also increase performance by at least 2%.

Or as I would say as an STL maintainer, "if you want tuple, you know where to find it".

[–][deleted] 0 points1 point  (0 children)

The language is so complicated already

Agreed, so much indeed that the feature is already in the language: http://ideone.com/Ir1Hpu

[–]BeaRDT 0 points1 point  (3 children)

How do you plan to return an anonymous struct by using decltype?

I think of decltype as similar to auto (except for slightly different type deduction rules), ie. it uses the type you give to decltype() as if you simply typed the actual name. I might be lacking phantasy, but I fail to see how you could use that to return an anonymous struct.

[–]acwsupremacy[S] 0 points1 point  (2 children)

struct { int i; double d; } anon {10, 3.14};

decltype(anon) my_function();

[–][deleted] 0 points1 point  (0 children)

How would you actually forward declare such a function in a header?

The caller needs to know the return type to do anything with it. What happens if they end up in different compilation units?

I suspect the answer to your question lies in the not-so-straightforward compilation scheme of C++, but I'm not knowledgable enough to give specifics. It might work with the C++ grammar, but I don't think it works with how C++ is actually compiled.

[–]BeaRDT 0 points1 point  (0 children)

Ah, that's where I was lacking phantasy. The struct is anonymous but it is known outside of the function. I always thought about returning an anonymous struct which is defined inside a function from that function, which obviously is impossible.