I was designing an RGBA
class, where four parameters needed to be passed to the class's constructor, to instantiate the class. The constructor looks like the one below:
RGBA(int red = 0, int green = 0, int blue = 0, int alpha = 255)
{
auto valid_color = [](int param) {return (param >= 0 && param <= 255) ? param : 0; };
m_red = valid_color(red);
m_green = valid_color(green);
m_blue = valid_color(blue);
m_alpha = valid_color(alpha);
}
As you can see above, I have used a lambda for each parameter to verify the passed paramater. This lead me to wonder, what advantages could such a lambda have over a function, such as this (declared private
in the the interface):
int validate_color(int param)
{
return (param >= 0 && param <= 255) ? param : 0;
}
So my options are this:
- Keep using the lambda, and let the code be.
- Instead of a lambda, declare an actual function, like the one above, and use that.
- Just write it out completely for everything. (Slightly tedious and definitelt error-prone).
Which option seems the best, and why?
Aucun commentaire:
Enregistrer un commentaire