8 Comments
Aug 1, 2022Liked by Jeff Schwab

"This post presents a lot of code as screenshots."

Does substack not provide a way to include fixed with code blocks at all? If the only limitation is not supporting syntax highlighting I'd probably give it up to be more accessible.

Expand full comment
author
Aug 1, 2022·edited Aug 1, 2022Author

Thanks, Dan. Next time there's a code-heavy post, I'll try Substack's code blocks. As you say, they do not support syntax highlighting, and the font is oddly large; but they certainly would be more accessible than screen shots for at least some readers.

Expand full comment
Jul 30, 2022Liked by Jeff Schwab

Cppcheck already warns for this code:

test.cpp:16:45: error: Using object that is a temporary. [danglingTemporaryLifetime]

assert((std::vector<int>{1, 2, 3, 4} == append34({1, 2}))); // FAIL: UB

^

test.cpp:3:12: note: Return lambda.

return [&](std::vector<int>&& items) {

^

test.cpp:2:50: note: Passed to reference.

auto make_appender(std::vector<int> const& suffix) {

^

test.cpp:4:36: note: Lambda captures variable by reference here.

return append(move(items), suffix);

^

test.cpp:15:35: note: Passed to 'make_appender'.

auto append34 = make_appender({3, 4});

^

test.cpp:15:35: note: Temporary created here.

auto append34 = make_appender({3, 4});

^

test.cpp:16:45: note: Using object that is a temporary.

assert((std::vector<int>{1, 2, 3, 4} == append34({1, 2}))); // FAIL: UB

Expand full comment
Jul 30, 2022Liked by Jeff Schwab

Would love to see Ada added to the comparison.

Expand full comment

Yes, c++ is a footgun :-) , but saying "just a weirdness" is a bit unfair to c++:

You can get a runtime warning by compiling with -fsanitize=address, undefined.

Moreover, you can actually make a proper make_appender using perfect forwarding:

template<typename S>

auto make_appender(S&& suffix)

{

return [perf_fwd_suffix = std::tuple{std::forward<S>(suffix)}](std::vector<int>&& items)

{

return append(std::move(items), std::get<0>(perf_fwd_suffix));

};

}

Expand full comment

The closure pattern in C++ is a very bad thing.

If you want to close over values, copy it.

Moving out of named objects is also a recipe for disaster.

Expand full comment

I'm blind and posting just the screenshots without the code in at least alt text or caption means that I'm not part of the audience.

Expand full comment
author
Jul 30, 2022·edited Jul 30, 2022Author

Thanks for the guidance! Each code image's caption now links to the raw text on GitHub. Further suggestions of how to do better are, of course, appreciated.

Expand full comment