

Show HN: C++11 inline object definitions - matt42
https://github.com/matt-42/iod

======
JoachimSchipper
Note that something like this is actually part of C99 (but not of C++ - the
C++ grammar was apparently too tortured to support this):

    
    
        struct example_t {
            int foo, bar, baz;
        } example = {
            .foo = 1,
            .bar = 42,
        };
        /* unnamed members are implicitly set to 0 / NULL */
        assert(example.baz == 0);
    

combined with compound literals, you can do stuff like

    
    
       example = (struct example_t){
          .foo = 2,
          .baz = 1
       };
       assert(example_cmp(&example,
          &(struct example_t){ .foo = 2 }) == 0);
    

It's one of C99's neater features, I think. I can imagine C++ programmers
liking something like iod.

------
yyyymmdd
actually: auto obj = [ x=1, y="foo" ]{};

~~~
reubenmorais
This just declares a lambda with two parameters with default values. You can't
do obj.x, you can't declare "objects" inside of obj, you can't return obj in
the parent scope.

Why would you ever do:

    
    
      [x=1, y="foo"]{
        // use x, y
      }();
    

When you can just do:

    
    
      auto x = 1;
      auto y = "foo";
      // use x, y
    
    ?

~~~
matt42
It is in the C++14 standard:

For every init-capture a non-static data member named by the identifier of the
init-capture is declared in the closure type. This member is not a bit-field
and not mutable.
[http://isocpp.org/files/papers/N3648.html](http://isocpp.org/files/papers/N3648.html)

~~~
thwest
Actually the member can still be declared `private:` and the `obj.a.name`
would not be portable. It appears GCC's implementation to make these members
`public:` is conforming to c++11 but do not rely on this behavior.

I've been told in ##C++ that the current C++14 draft says: `An init-capture
behaves as if it declares and explicitly captures a variable of the form “auto
init-capture ;” whose declarative region is the lambda-expression’s compound-
statement`

This change would make the named member access non-conforming

------
zedpm
This is interesting, but I can't help but wonder at the wisdom of writing C++
in this fashion. What's the use case for this? I perhaps naively think that
the problem domains for C++ and Javascript are very different, and that this
sort of thing may make sense in the domains where C++ doesn't make sense.

I think I'd be frustrated if I was working on someone's C++ code and I found
objects springing up out of nowhere like that.

~~~
chillingeffect
That is a laughably narrow worldview. It's even sadder that instead of
reasoning out why some domains don't make sense, you direct a burden on the
community to persuade you. After matt-42 took the time to create this, present
it and share it, you're scoffing at him and challenging everyone else to open
up your closed mind. I'm content to leave you confused in the corner, but
you've sprayed your little pee on the beautiful garden where I walk each day
so I'll give you a tip merely so it smells better to me and the rest my
colleagues.

My tip isn't about iod. It's this: Try re-approaching your evaluation of iod
with the worldview that IS useful and sketch out some scenarios. If you can
show that it doesn't make sense, you'll engage someone intelligent in a
conversation. If you won't invest that time in yourself, don't expect anyone
else to invest their time in you.

> I think I'd be frustrated if I was working on someone's C++ code and I found
> objects springing up out of nowhere like that.

That sounds like a personal problem having to do with self-control. You should
get a blog.

Speaking of which, the website listed in your profile, c10n.net, is down.

~~~
zedpm
I find your vitriol unwelcome and inappropriate for a discussion that could be
and ought to be constructive. I could spend some time explaining my position
and defending my previous comment to you, but you haven't earned that.

I will note, however, that questions like the one I posed are in general
useful to open up discussion about something new like this. Instead of viewing
it as an attack, see it as initiating some exposition on specific cases where
this would be useful.

~~~
chillingeffect
> vitriol > Instead of viewing it as an attack, see it as initiating some
> exposition on specific cases

And you aren't you taking your own advice because why?

If your comment is supposed to initiate some exposition, _then fucking expose
away_ or buzz off and fix c10n.net. All you did was throw up your arms and say
"oh, I don't know if this is that great," without even trying it or pointing
out why and expect others to fall in line below you and do all the work for
you...

And you can see the line of commenters telling you why iod /IS/ a good idea.
You could have figured this out on your own, but instead you chose to spend
your time getting others to give you the answers.

Ever wonder why they call this website Hacker News and not Manager News?

Asking others to figure out everything for you and report back is unwelcome
and inappropriate. Commenting "I don't think this is a good idea, but I'm not
going to say why" is unwelcome and inappropriate. You say "I could spend some
time explaining my position and defending my previous comment to you"
Bullshit. You don't have anything valuable to share except a regrettable knee-
jerk reaction after you saw a little JSON in C++.

You even admitted to "perhaps thinking naive." Own up to it. You just tried to
cast doubt on something you know nothing about and you're getting called out
on it in a more direct way than anyone has ever given you feedback before.

And I don't care if you find me unwelcome. I am, in fact, brutal and I dislike
you because you're a lazy bitch who doesn't contribute and can't keep your
website running. And I DO have something to say about iod which is more than
you have:

I think iod is a brilliant effort. I will enjoy making C++ programs with
inline-defined JSON to connect to my server and run it through all kinds of
configurations. I don't want to touch Javascript to do that because I think
Javascript is shit. Thank you matt-42.

p.s. It didn't compile for me under cygwin with the command line "g++
-std=c++11 example.cc" because the symbol "iod_extend" is missing. I commented
out line 42 and it built and ran fine.

How many other fucking people in this thread even TRIED the code? They would
have found the same bug as me if they had.

~~~
zedpm
You've managed to break the first two rules in the commenting guidelines [1];
it seems you're in no position to advise anyone on appropriate commenting on
HN. Profanity, insults, and personal attacks simply don't belong here. Aside
from your childish outbursts, we've had a productive conversation in this
thread.

[1]
[http://ycombinator.com/newsguidelines.html](http://ycombinator.com/newsguidelines.html)

~~~
chillingeffect
Where the rule says "Don't say things you wouldn't say in a face to face
conversation," you imagine I would not use profanity in a face-to-face
conversation.

I will _gleefully_ reiterate everything I've said here in a face-to-face
conversation.

I note that you have changed your tune toward the author ("I probably should
have started out my initial comment by saying thanks") suggesting you're
learning submitters' feelings outrank commenters'. There is hope for you yet.

~~~
dang
Please don't make any more personally aggressive comments on Hacker News.

------
TillE
That's surprisingly concise and not _too_ hackish, especially compared to some
other techniques I've seen for adding basic reflection to your C++ classes.

~~~
matt42
This is thanks to the C++11 variadic templates that symplifies (a lot) the
writting of such abstractions. The object model is only 100 lines of C++11:
[https://github.com/matt-42/iod/blob/master/iod.hh](https://github.com/matt-42/iod/blob/master/iod.hh)

------
greggman
This is awesome! I was just thinking of porting some JavaScript based stuff to
C++ and not looking forward to dealing with all my adhoc JSON stuff and having
to write classes for everyone of them.

------
Deregibus
Very cool. Anonymous types can be really handy in C#, especially when dealing
with LINQ to objects code. This looks like it could be a similar fit.

