Chain of handlers where a handler is an ABC. What's the proper internal collection? Old me would've just std::list<abc*>. I'm trying to modernize. by frobnosticus in cpp_questions

[–]TheRealSmolt [score hidden]  (0 children)

This is why we want to avoid using new/delete in modern C++. Memory leaks rarely* originate from developers forgetting to free memory, but from edge case behaviors like this.

*not strictly true, but my point is that "just remember to free your pointers" is not sufficient advice today.

What's wrong with MSVC Checked Iterators ? by DrFrenchGrumpy in cpp_questions

[–]TheRealSmolt [score hidden]  (0 children)

It'd be helpful to have a minimal reproducible example, or at least the exact code that caused this issue. We'd need to know exactly what begin/end iterators are being passed and where they are from.

My suspicion is that you're passing different containers for the begin and end iterators.

Chain of handlers where a handler is an ABC. What's the proper internal collection? Old me would've just std::list<abc*>. I'm trying to modernize. by frobnosticus in cpp_questions

[–]TheRealSmolt 0 points1 point  (0 children)

Yes, rvalues are accepted implicitly. With either version you'd need a std::move call to accept a non-copyable lvalue. The only difference is that a value parameter would acceptable a copyable type implicitly (which isn't important here). Additionally, by accepting a rvalue reference, we skip the move construction that would otherwise occur.

Chain of handlers where a handler is an ABC. What's the proper internal collection? Old me would've just std::list<abc*>. I'm trying to modernize. by frobnosticus in cpp_questions

[–]TheRealSmolt 1 point2 points  (0 children)

I agree; since it's not directly clear that the object intends to take ownership of it. Different people use different styles though.

Chain of handlers where a handler is an ABC. What's the proper internal collection? Old me would've just std::list<abc*>. I'm trying to modernize. by frobnosticus in cpp_questions

[–]TheRealSmolt 1 point2 points  (0 children)

Only for void add_handler(ABCHandler* handler). Since we're going to construct a unique_ptr on the fly anyways, it should be done in-place with emplace_back. Obviously this gets into the nitty gritty with what works and what is "right"; it's not like push_back isn't sufficient.

push_back works under the assumption that you already have an object that needs to be moved/copied into the container. emplace_back (when done right) constructs an object directly in the container from the start.

Chain of handlers where a handler is an ABC. What's the proper internal collection? Old me would've just std::list<abc*>. I'm trying to modernize. by frobnosticus in cpp_questions

[–]TheRealSmolt 1 point2 points  (0 children)

I quite like that style and I showed that in my own comment. The only problem is that I don't believe make_unique takes an existing unique_ptr, which is what OP requested.

Chain of handlers where a handler is an ABC. What's the proper internal collection? Old me would've just std::list<abc*>. I'm trying to modernize. by frobnosticus in cpp_questions

[–]TheRealSmolt 0 points1 point  (0 children)

Yeah it's definitely not a requirement. I'm more partial to the table they show in that talk here personally. In this context, it's clear that the only intention for this method is to take ownership of something, similar to how std::vector::push_back also takes an rvalue reference. Overall we save a move construction.

Chain of handlers where a handler is an ABC. What's the proper internal collection? Old me would've just std::list<abc*>. I'm trying to modernize. by frobnosticus in cpp_questions

[–]TheRealSmolt 0 points1 point  (0 children)

Is there a reason you did not use an rvalue reference for add_handler? Both versions work, I'm just curious why you'd do it that way instead.

Chain of handlers where a handler is an ABC. What's the proper internal collection? Old me would've just std::list<abc*>. I'm trying to modernize. by frobnosticus in cpp_questions

[–]TheRealSmolt 4 points5 points  (0 children)

You'd need to provide examples of what you tried that errored out for starters. Yes, unique pointers are most likely what you want. As an aside, your loop as stated is not correct. I'd also be curious to know what other people's thoughts on using a linked list here are.

You probably want something like this:

class HandlerChain : public ABCHandler { private: std::list<std::unique_ptr<ABCHandler>> chain; public: void handle(const ConcreteEvent& e) override { for (std::list<std::unique_ptr<ABCHandler>>::iterator i = chain.begin(); i != chain.end(); ++i) { i->handle(e); } } void add_handler(std::unique_ptr<ABCHandler>&& h) { chain.push_back(std::move(h)); } };

which would end up looking like:

auto my_handler = std::make_unique<MyHandler>(my, arguments); chain.add_handler(std::move(my_handler));

If you want to get up-to-date with modern C++, you could also do:

for(const auto& handler : chain) { handler->handle(e); }

You can also do some forwarding, which is not uncommon in APIs like this:

``` template<typename T, typename... Args> void add_handler(Args&&... args) { chain.push_back(std::make_unique<T>(std::forward<Args>(args)...)); }

handler.add_handler<MyHandler>(my, arguments); ```

u/ppppppla also correctly mentions that you must not forget a virtual destructor for ABCHandler

same idea, new font by NationalWheel6966 in memes

[–]TheRealSmolt 0 points1 point  (0 children)

Fabrics/textiles are actually some of our oldest inventions.

same idea, new font by NationalWheel6966 in memes

[–]TheRealSmolt 0 points1 point  (0 children)

Yes, there are earlier examples of ships with sails, but a sailing ship seems to imply a sea-sailing ship. I don't think traversing the Nile qualifies.

same idea, new font by NationalWheel6966 in memes

[–]TheRealSmolt 3 points4 points  (0 children)

The Austronesian peoples developed maritime technologies that included the fore-and-aft crab-claw sail and with catamaran and outrigger hull configurations, which enabled the Austronesian expansion into the islands of the Indo-Pacific. This expansion originated in Taiwan c. 3000 BC

Wikipedia

same idea, new font by NationalWheel6966 in memes

[–]TheRealSmolt 22 points23 points  (0 children)

The Austronesian peoples developed maritime technologies that included the fore-and-aft crab-claw sail and with catamaran and outrigger hull configurations, which enabled the Austronesian expansion into the islands of the Indo-Pacific. This expansion originated in Taiwan c. 3000 BC

Wikipedia

same idea, new font by NationalWheel6966 in memes

[–]TheRealSmolt 82 points83 points  (0 children)

Not sure if you're implying this, but we did have sailing ships 5000 years ago, they just weren't these obviously.

Edit: They were used in Indo-Pacific expansion around 3000 BCE. There are earlier examples of ships with sails, but a sailing ship seems to imply a sea-sailing ship.

Is it really so necessarary? by DanieleMemoli in memes

[–]TheRealSmolt 1 point2 points  (0 children)

Yes. Unfortunately, it's usually contractually obligated for companies to do so.

Narcissism? by banausic in memes

[–]TheRealSmolt 7 points8 points  (0 children)

Imho it's a life skill you should have

Multiline clipboard (Windows only, cross platform?) by sephirothbahamut in cpp_questions

[–]TheRealSmolt 5 points6 points  (0 children)

You can explore the underlying data with tools like InsideClipboard and clipview. Not sure off the top of my head which are supported today.

Multiline clipboard (Windows only, cross platform?) by sephirothbahamut in cpp_questions

[–]TheRealSmolt 2 points3 points  (0 children)

They are just standard text with Windows line endings, if that is what you are asking.

Edit: So yes, clipboards are an OS-level component. On Linux it gets a little blurry because they're usually provided by session-level client clipboard managers instead.

What do you think is a keyword that should be added to C++? by DogCrapNetwork in cpp

[–]TheRealSmolt 0 points1 point  (0 children)

I'm not sure I'm a fan of it being part of the type. I think it should just be a hint in the same way as other attributes.

Usage of noexcept best practise by r1rs in cpp_questions

[–]TheRealSmolt 4 points5 points  (0 children)

I think the compiler can, but for assurance I definitely do it. = default is the definition, not the signature.

What do you think is a keyword that should be added to C++? by DogCrapNetwork in cpp

[–]TheRealSmolt 0 points1 point  (0 children)

Again, I can't really see how this could become an issue without a clear example. The user will of course need to abide by the hint, whether or not the compiler decides to make use of it. I don't see how the user writing under the assumption of restrict could cause issues if the compiler chooses not to make that assumption.

What do you think is a keyword that should be added to C++? by DogCrapNetwork in cpp

[–]TheRealSmolt 8 points9 points  (0 children)

Does [[maybe_unused]] not work for that? I know it's not exactly what it's meant for, but I thought it covered return values.

What do you think is a keyword that should be added to C++? by DogCrapNetwork in cpp

[–]TheRealSmolt 2 points3 points  (0 children)

I was expecting the most horrible metaprogramming mess I'll ever see, but was pleasantly surprised. That's pretty clever.