C++ Logo


Advanced search

Default arguments for operators

From: Joseph Malle <malle_at_[hidden]>
Date: Tue, 11 Feb 2020 17:59:39 -0500
Currently, operators do not support default arguments in most cases and
must have a predetermined number of arguments.

For example, operator/ must have two arguments and those arguments cannot
have defaults. I propose allowing such operators to have additional
arguments as long as they have defaults. The default arguments must come
after the regular arguments.

auto operator/(U, V) // OK
auto operator/(U, V, W) // Error, and should remain an error
auto operator/(U, V = V(), W) // Error, and should remain an error

auto operator/(U, V, W = W()) // Error, but should be ok

I don't think operator() would need to change at all as it already supports
default arguments. It would now need wording to allow defaults in any
order. Perhaps there are other operators with special cases that I haven't
thought of.

The reason I want to have this feature is to use std::source_location with
operators. As far as I can tell, default arguments are the only sensible
way to use std::source_location.

I think this is a backwards compatible change (but perhaps it could be
detected by concepts? not 100% sure).

Feedback welcome.


Received on 2020-02-11 17:02:30