C++ Logo

STD-PROPOSALS

Advanced search

Subject: [std-proposals] Proposal: template function solution to Pythonic optional function arguments
From: Ed Bird (HYPER-NOVA_at_[hidden])
Date: 2020-07-19 10:45:35


Hi,

I am not sure if this is the correct email address to which I should direct this enquiry, however -

while working on some code today it occurred to me that it would be useful to have some implementation of pythonic optional arguments.

This is almost certainly not a new idea.

However, the following proposal for how to implement this might be useful, although some experts would need to comment.

In my view, if a function has an optional argument, then it should be treated similarly to how template classes are treated. In other words, this is an example of a case where one block of code should produce multiple blocks of executable code when compiled.

Consider this function

void draw(const int parameter, opt const int mode)
{
    #if OPT mode
        if(mode == 1)
        {
            do_draw(-1);
        }
        else
        {
            do_draw(parameter);
        }
    #else
        do_draw(parameter);
    #endif
}

this would then be used as follows

draw(param, mode=-1);

`mode=...` would only be required for optional arguments, which have to go after all regular arguments (just my proposal) ...

a new C++ keyword "opt" (short for optional) is introduced, and then a somewhat messy construct of `#if #else #endif` compiler directives are used to instruct the compiler what code to generate depending on the optional arguments.

Just a couple of comments

I use this example because it is closely related to the code I happened to be working on

Its possible something like this already exists in the latest versions of C++ but if so I am not aware of it

I don't personally like the syntax of using `#if` to achieve the anticipated result, but this is just my personal opinion

I am by no means a compiler expert so perhaps there are technical reasons as to why this is not possible or undesirable to implement

I might have sent this email to completely the wrong place

My example code is not "good" code. I happen to be hacking around with some functions where (mostly for rapid prototyping purposes) it would be convenient for me to be able to include optional arguments and have the function behave differently if these exist. If I were to clean up my functions I would probably do this differently, but optional arguments might be one way of accomplishing what I am trying to do.

I actually introduced a bug due to the fact that I had a long list of default parameters, and missed one when calling the function. An implicit cast from int to bool was responsible for the function behaving differently to how I expected. I fixed this by putting in the missing arguments, of course. But it wouldn't have happened if I could have done `draw(1, mode=-1);`

Best Regards,
hyper-nova



STD-PROPOSALS list run by herb.sutter at gmail.com

Standard Proposals Archives on Google Groups