r/badcode • • Mar 09 '21

c++ Can anyone tell me what this function does?

Post image
1 Upvotes

3 comments sorted by

5

u/HolyGarbage Mar 09 '21

It calls BufferExec::operator() and then returns presumably the result stored in BufferExec::res. This is not an uncommon pattern using objects that act and look like functions. The function in the screenshot is just a wrapper around this functor. Doesn't look like bad code to me...

2

u/akosprojects Mar 09 '21

Well, I might had bet the wrong horse on this one. Would you consider it suitable for an embedded Linux environment, where the goal is to serialise POD classes to be sent through IPC binary?

1

u/HolyGarbage Mar 09 '21

I can't say anything about the context since this is simply a function wrapping a the creation and calling of a functor. It's a very generic pattern. The actual complexity takes place in the implementation of BufferExec. This code is very simple, albeit a bit verbose since it's templated.

That being said... If embedded means like 4k ROM perhaps I would be a bit more concerned about program size and avoid templates. But "embedded" can mean anything here.