Post 9hIMHykYWSkf5l6Lsu by mdhughes@cybre.space
 (DIR) More posts by mdhughes@cybre.space
 (DIR) Post #9hIMHydSwp52jlmgpk by uliwitness@mastodon.technology
       2019-03-30T11:23:12Z
       
       0 likes, 0 repeats
       
       I have a common pattern:stringstream err;err << “Can’t do” << x << “ with a ” << y;log(err.str());throw runtime_error(err.str());Can I somehow simplify this? Maybe likelogAndThrow() << “Can’t do” << x << “ with a ” << y;?
       
 (DIR) Post #9hIMHykYWSkf5l6Lsu by mdhughes@cybre.space
       2019-03-30T11:27:21Z
       
       0 likes, 0 repeats
       
       @uliwitness I dunno the C++isms, but in C you can do something like:#define ERRLOG(_msg_) do { fprintf(stderr, _msg); exit(1); } while(0);
       
 (DIR) Post #9hKCoY2wDSOAbSVjyS by uliwitness@mastodon.technology
       2019-03-31T08:50:38Z
       
       0 likes, 0 repeats
       
       @mdhughes Yeah I try to avoid macros where I can. Too many chances to evaluate arguments twice, or make mistakes when used in if/then constructs (like your extra semicolon after the while(0) here).
       
 (DIR) Post #9hKDL5XVE2ItkLK892 by mdhughes@cybre.space
       2019-03-31T08:56:31Z
       
       0 likes, 0 repeats
       
       @uliwitness Extraneous but harmless, since ERRLOG("foo"); will just end ;; which is fine. Getting args and PRETTY_FUNCTION in there is left as an exercise for the reader, but that's how you build new language constructs in C.
       
 (DIR) Post #9hKGAiczr1kfeedlyq by uliwitness@mastodon.technology
       2019-03-31T09:28:15Z
       
       0 likes, 0 repeats
       
       @mdhughes It's a problem if used in the "if" clause of an if/then/else. If you don't put braces around your if statements, the else might associate with the wrong thing. Also, in -Werr builds, this will give an empty statement warning.
       
 (DIR) Post #9hKGPscoy28v0JmuDA by uliwitness@mastodon.technology
       2019-03-31T09:30:02Z
       
       0 likes, 0 repeats
       
       @mdhughes Isn't that the whole point of using do {} while(0) in your macro definitions? To have something that requires that semicolon just like a real function?
       
 (DIR) Post #9hKGPsjYYzWxLCwHi4 by mdhughes@cybre.space
       2019-03-31T09:31:00Z
       
       0 likes, 0 repeats
       
       @uliwitness Mostly it just lets you have multiple statements and a new variable scope (in C99+) if you need it.
       
 (DIR) Post #9hKHfx6GCJXDTCml3g by uliwitness@mastodon.technology
       2019-03-31T09:35:19Z
       
       0 likes, 0 repeats
       
       @mdhughes Not trying to harp on what was obviously just a typo on code written in a status, but it illustrates very well one of the subtleties of why I try to avoid macros in C++ unless I need to stringify or token-paste.
       
 (DIR) Post #9hKHfxFpcjBtwtGOye by uliwitness@mastodon.technology
       2019-03-31T09:40:50Z
       
       0 likes, 0 repeats
       
       @mdhughes That and the naïve version of FOO(msg) do { log(msg); throw runtime_error(msg); } while(0) possibly evaluating msg twice (and thus changing behaviour if msg involved e.g. popping the error message off a stack).Of course, in plain C you sometimes have no choice, but macros are always a last resort after inline functions and templates for me.
       
 (DIR) Post #9hKHfxU0m0X8ertj4y by mdhughes@cybre.space
       2019-03-31T09:45:06Z
       
       0 likes, 0 repeats
       
       @uliwitness I'm also heavily shaped by Scheme where macros are more powerful and the way you customize your tools. C's macros suck, but they can do the job, you just have to be careful and read the generated output sometimes.