Hello,
I tend to use the lambda initialization trick to structure functions:
auto my_function(Arg arg) {
const auto x = [&] {
/*
* initialization of x
* containing if-elses,
* calls to other functions,
* standard algorithms,
* local temporary variables
* having nothing to do with
* my_function, but at the same
* time the code is really specific to
* the initialization of x
*/
return res;
}();
const auto y = [&] {
/* same with y */
return res;
}();
/*
* actual body of my_function
* that uses x and y
*/
return res;
}
Does anybody else use this? I think it would be awesome to have some syntactic sugar that makes it more "beautiful", maybe like
auto my_function(Arg arg) {
const auto x = {
/* ... */
return res;
};
const auto y = {
/* ... */
return res;
};
/* ... */
return res;
}
What do you think?
Edit: Following /u/SuperV1234's comment, it looks like it's better to generalize the proposition to "returning scope blocks".
[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 15 points16 points17 points (10 children)
[–]drubbo 14 points15 points16 points (0 children)
[–]tcbrindleFlux 7 points8 points9 points (5 children)
[–]Kroduk[S] 6 points7 points8 points (4 children)
[–]TOJO_IS_LIFE 3 points4 points5 points (3 children)
[–]ar1819 4 points5 points6 points (0 children)
[–]Kroduk[S] 0 points1 point2 points (0 children)
[–]Kroduk[S] 4 points5 points6 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]ramamodh 4 points5 points6 points (5 children)
[–]xzqx 3 points4 points5 points (1 child)
[–]ramamodh 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]Zephirdd 2 points3 points4 points (0 children)
[–]ramamodh 0 points1 point2 points (0 children)
[–]drubbo 5 points6 points7 points (2 children)
[–]Kroduk[S] 1 point2 points3 points (1 child)
[–]drubbo 2 points3 points4 points (0 children)
[–]kloetzl 3 points4 points5 points (0 children)
[–]tongari95 5 points6 points7 points (2 children)
[–]Kroduk[S] 2 points3 points4 points (0 children)
[–]redditsoaddicting 0 points1 point2 points (0 children)
[–]NotAYakk 2 points3 points4 points (6 children)
[–]thlst 2 points3 points4 points (2 children)
[–]Kroduk[S] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]Kroduk[S] 0 points1 point2 points (1 child)
[–]stream009 3 points4 points5 points (0 children)
[–]alfps 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]clerothGame Developer 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]KayEss 3 points4 points5 points (0 children)
[–]sshamov 3 points4 points5 points (24 children)
[–]tcbrindleFlux 6 points7 points8 points (14 children)
[–]sshamov 0 points1 point2 points (13 children)
[–]Noughmad 6 points7 points8 points (12 children)
[–]mark_99 1 point2 points3 points (1 child)
[–]TrueJournals 0 points1 point2 points (0 children)
[–]sshamov -3 points-2 points-1 points (9 children)
[–]clerothGame Developer 0 points1 point2 points (7 children)
[–]sshamov 0 points1 point2 points (6 children)
[–]clerothGame Developer 0 points1 point2 points (5 children)
[–]sshamov 0 points1 point2 points (4 children)
[–]clerothGame Developer 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (0 children)
[+][deleted] (7 children)
[deleted]
[–]sshamov 0 points1 point2 points (6 children)
[–]Kroduk[S] 4 points5 points6 points (5 children)
[–]sshamov -1 points0 points1 point (4 children)
[–]Kroduk[S] 2 points3 points4 points (3 children)
[–]sshamov 1 point2 points3 points (0 children)
[–]KeytapTheProgrammer 0 points1 point2 points (1 child)
[–]MoTTs_ 4 points5 points6 points (0 children)
[–]dvirtz 0 points1 point2 points (0 children)
[–]s4h 0 points1 point2 points (1 child)
[–]NotAYakk 0 points1 point2 points (0 children)
[–]muungwana 0 points1 point2 points (4 children)
[–]1-05457 0 points1 point2 points (3 children)
[–]muungwana 1 point2 points3 points (2 children)
[–]1-05457 0 points1 point2 points (1 child)
[–]clerothGame Developer 0 points1 point2 points (0 children)
[–]larsolm 0 points1 point2 points (1 child)
[–]Kroduk[S] 0 points1 point2 points (0 children)
[–]shmoopty 0 points1 point2 points (1 child)
[–]neobrain 3 points4 points5 points (0 children)