www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - functions allowed to overload on const int vs int vs immutable int? +

this compiles, but equivalent in C++ (const int vs int) would give a
compile error (error: redefinition of 'fun'); what's the rationale for
allowing these overloads?

void fun(int src){ writeln2(); }
void fun(immutable int src){ writeln2(); }
void fun(const int src){ writeln2(); }

void main(){
  {int src=0; fun(src);} // matches fun(int)
  {immutable int src=0; fun(src);} // matches fun(immutable int)
  {const int src=0; fun(src);} // matches fun(const int)

The spec does mention `match with conversion to const` taking
precedence over `exact match` however this isn't precise: the
following would be more precise instead (at least according to snippet
`match with conversion to/from const/immutable/mutable`


Functions are overloaded based on how well the arguments to a function
can match up with the parameters. The function with the best match is
selected. The levels of matching are:

no match
match with implicit conversions
match with conversion to const
exact match
Jan 26