Opened 6 months ago
Closed 4 months ago
#292 closed defect (fixed)
Constant Reference Argument Binding Update
Reported by: | ajbeach | Owned by: | |
---|---|---|---|
Priority: | critical | Component: | cfa-cc |
Version: | 1.0 | Keywords: | |
Cc: |
Description ¶
The following code does not work currently:
int intref(int const & x); int main(int argc, char * argv[]) { intref(0); }
It seems like a perfect match, but it is actually not, because the 0 actually is an instance of zero_t. The matching rules require an exact match, like a mutable reference instead of like a value.
Now, exact matches being required for mutable references makes sense because we want to bind the reference to the exact memory location, and have it act as an input-output parameter. But constant references only do input so a conversion is safe because the address doesn't have to refer to a meaningful output location. This means its use case is more like pass-by-value than pass-by-reference even though the reference type is used.
There are still some reasons to avoid the implicit conversion here, such as to maintain the symmetry between pointers and references. However there is a massive argument for it and that is we would like to be able to write:
// In prelude: New pattern for copy constructors: void ?{}(signed int &, signed int const &); // In user code: int counter = 0;
In fact, unless we find another way to solve the recurive copy problem, we will have to do this eventually. But currently, we need to copy a type in order to copy it and that has some issues.
We will also have to examine rvalue to reference conversion, because this case does in fact work, even though a warning message is issued:
intref(2 + 3);
Should be working for now.