C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Parameters with default values at arbitrary positions, with empty arguments

From: Rainer Deyke <rainerd_at_[hidden]>
Date: Fri, 27 Nov 2026 09:25:33 +0100
On 11/26/26 21:29, Bo Persson via Std-Proposals wrote:
> If you have 3 default parameters, all in the wrong order, something is
> seriously wrong. I would refactor that code to make it better.

I don't think it's unusual at all to have a function with three or more
parameters. And I don't think it's unusual at all to have independent
default values for every one of these parameters. Consider:

   sprite::draw(
     double alpha = 1.0,
     double scale = 1.0,
     double rotation = 0.0);

By default, you don't specify any of these parameters. But sometimes
you want to draw a scaled sprite (without specifying rotation or alpha),
sometimes you want to draw a rotated sprite (without specifying alpha or
scale), sometimes you want to draw a semi-transparent sprite (without
specifying scale or rotation), and sometimes you want to specify two of
them or all three.

You can, of course, work around this limitation with a struct, but it
involves more code:

   struct sprite_draw_parameters {
     double alpha = 1.0;
     double scale = 1.0;
     double rotation = 0.0;
   };

   sprite::draw(sprite_draw_parameters);

   sprite spr;
   spr.draw({.alpha = 0.5});
   spr.draw({.scale = 2.0});
   spr.draw({.rotation = 90.0});


-- 
Rainer Deyke - rainerd_at_[hidden]

Received on 2026-11-27 08:25:42