lambda functions vs functors [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
when I am going through the lambda function, I have seen people comparing lambda with functors & I came across a statement
users don't have to clutter their code with small functors in some accessible scope.
My doubt is
what is the problem in having small functors in some accessible scope
isn't it good idea to have a single function (functor actually) & reuse it across multiple files in our project.
Thanks.

it is unnecessary if you have to use each only once. Lambdas usually makes the code more readable, the function is defined exactly at the place it is needed.
this is not always the case, a function may be called at only one place. Of course if you needed it at different places a functor may be more appropriate.

Related

Is it okay to shorthand a function call using a macro? C++ [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
I am slowly writing an emulator for a gameboy using C++, I am currently working on the CPU side of things. I have written a generic function that takes any two registers of the CPU and returns as a Word data type. As it is necessary to access individual registers and also a combination of registers.
const Word get_word(Byte *registerOne, Byte *registerTwo)
{
return ((*registerOne << 8) | *registerTwo);
};
calling this function gets tedious as you have to specify each register
get_word(&this->registers.h, &this->registers.l)
My question is if it okay to define a macro like so
#define get_HL() get_word(&this->registers.h, &this->registers.l)
since now I can call it using
get_HL()
The reason why I want to do it like this since I don't want to create more private/public functions that just perform function calls.
I have tried compiling and it seems to work as it should since its just a pre-processor macro but I am not sure of the design implication
EDIT:
Okay I mean there are glaring flaws with this and you should just make a function, just as much work to make a function or write a macro.
const Word get_HL() { return this->get_word(&this->h, &this->l); };
Let this be a post for people who had the same idea and hopefully stop making the same mistake
No, this isn't OK in my opinion. It hides what arguments you're passing into the function, and macros don't respect scopes and as such are highly susceptible to name conflicts. This seems like an ideal use case for a non-static member function.

What is the best way to pass parameters to a callback function? (C/C++) [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I'm learning SDL right now and I'm trying to use timer callback function.
The function SDL_AddTimer() offer me only one argument to pass through but I want to pass different types of variables into it.
(https://wiki.libsdl.org/SDL_AddTimer)
I think of one solution is that I can declare a structure containing all of my variables but I'm not sure if it is the best way to do so.
Thank you for your help ~~
I think of one solution is that I can declare a structure containing all of my variables but I'm not sure if it is the best way to do so.
Yes this is the correct and best way to pass parameters whenever you are presented with a callback that accepts void*. Then cast back to the struct type from inside the callback.
Same thing goes when using pthreads/Windows threads or other such common APIs.

Should a class be thread-safe? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Should a thread-safe mechanism be added when a class is developed and it is known that this class will be used in multi-threaded environment (not always however) or leave it to the user?
As a general rule, it's more flexible to leave it to the user. For example, consider a map-type container. Suppose the application needs to atomically move something from one map to another map. In this case, the user needs to lock both maps before the insert-erase sequence.
Having such a scenario be automatically taken care of somehow by your class would probably be inelegant, because it's naturally something that happens across objects, and because there may be many such scenarios, each slightly different.

Naming a method which does several actions [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I know that one method should be responsible for one action (or so I've been told) but let's it can't be avoided (or workaround is impractical). Imagine that a game of snake is being developed and it has to have two methods: one which simply moves snake forward and one which increments the length of the snake by one and moves it. First method could just be named move(), but how about the second one? Simpliest solution is to use and, but is it a good practice? For example: incrementAndMove(). What are the alternatives?
I would choose the name GrowAndMove, but semantically it is the same as what you propose. Still implement this method to call two separate methods - Grow and Move to keep each method responsible for a single aspect of the changes happening during a single step.

C++ Class functions return values VS operate on local data? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Suppose that we have class C and our process is as follows
do func1
do func2
do func3
do func4
where each function operates on the data from the previous stage
Which is better from the point of view of system design?
make each func takes an input, returns its results and pass the result to the next stage
make each func operate on data members in the class c and they all return void
And if these 2 strategies are famous design patters what is the name of each design pattern?
If you use option 2, and you'll pass the code to another developer. If he/she accidentally calls func3(), func4() in the middle of the code, it will be logically wrong.
If you use option 1, the developer needs to think of the parameters which will be passed through. It is less likely that he will make mistake.
Therefore, I'll go with option 1.