Re:I ruined my vacation by reverse engineering WSC
by es3n1n on Monday May 12, 2025 @03:13AM
(#43960376)
yeah sorry i didnt feel like implementing my own RAII stuff for all the COM thingies due to time constraints. it will be changed in the next update though
[ Parent
| on HN ]
Re:I ruined my vacation by reverse engineering WSC
by junon on Monday May 12, 2025 @03:26AM
(#43960445)
Honestly if this isn't part of a public API this isn't very cursed in terms of C++, especially if you have a lot of one-off cleanup operations.
I think the only bit I don't like personally is the syntax. I normally implement defer as a macro to keep things clean. If done correctly it can look like a keyword: `defer []{ something(); };`.
Re:I ruined my vacation by reverse engineering WSC
by quietbritishjim on Monday May 12, 2025 @06:38AM
(#43961545)
I think the syntax is exactly why they're saying it's cursed. IMO your suggestion is no better - yes it makes defer look like a keyword, but it's not! As I said in a sibling comment, I think it's clearer if you're honest that you're using a macro: DEFER([](){something();});
Or you could even make a non-macro version (but then you need to think of variable names for each defer):
auto defer_uninitialise = do_defer([](){CoUninitialize();});
Re:I ruined my vacation by reverse engineering WSC
by junon on Monday May 12, 2025 @08:03PM
(#43968584)
Sure, I've used __LINE__ for this before too, and yeah I agree that my keyword construction was too clever (seemed cool at the time, since the macro had a dangling = at the end to make it work).
Re:I ruined my vacation by reverse engineering WSC
by Asooka on Monday May 12, 2025 @09:09AM
(#43962511)
Why did you write it with two structs though? You could do
#define defer(body) DeferHolder COMMON_CAT(_defer_instance, __LINE__) {([&]()->void body)};
and call it as defer({
function body here;
});
Which looks much nicer. The preprocessor treats balanced curlies as one single token regardless of how many lines it spans, precisely to enable this usage.
Re:I ruined my vacation by reverse engineering WSC
by gavinray on Monday May 12, 2025 @09:35AM
(#43962718)
https://en.cppreference.com/w/cpp/experimental/scope_exit
scope_exit{[&]{ ... } };