Show HN: C++11 inline object definitions
github.com
github.com
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.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
?auto obj = [ x=1, y="foo" ]{}; obj.x; obj.y;
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
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
auto obj = [ name = 1, a = [name = 1]{} ]{}; obj.a.name;
test.cc: In function ‘int main()’: test.cc:5:38: warning: already captured ‘name’ in lambda expression auto obj = [ name = 1, a = [name = 1]{} ]{}; ^ test.cc:7:9: error: ‘struct main()::<lambda()>’ has no member named ‘name’ obj.a.name;
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.
job security and returning more than 1 object from a function without return pointers in the args
Actually that’s solved by returning `std::tuple` and `std::tie` – or using custom classes.
How is this any different than needing to know the order in which the arguments are passes? (Which I happen think is a problem that needs solved.)
fun(iod(arg1_ = 1, arg2_ = "foo"));
or by using method chaining :
http://www.parashift.com/c++-faq-lite/named-parameter-idiom....The other big advantage of this construct it that it provides introspection on objects at compile time, thus allowing to generate super fast serializers and deserializers without having to annotate the class.
Thanks also for taking a bit more time to explain how you see it being used. Between your comments and some of the other constructive replies, it makes more sense to me now.
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.
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.
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.
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.
Good ideas and creators will be able to withstand such scrutiny without any problems at all. Bad ones can use it as an opportunity to improve.
That's just, like, your opinion, man.
Frankly I find this hilarious. It looks ideal for hard-coded config information and test data, but the reality is that this wasn't all that complex to begin with...
So, one of the fun applications that I can think of for this is essentially a simple, short Scheme VM along the lines of chapter 4 of Abelson and Sussman.
But yeah, I would take a long, hard look at the situation before using this for real work. But then again, I wouldn't touch C++ for real work in the first place.
fun(iod(arg1_ = 1, arg2_ = 4)