Biohazrd icon indicating copy to clipboard operation
Biohazrd copied to clipboard

Fix ambiguous overloads differing only by const-ness

Open PathogenDavid opened this issue 3 years ago • 1 comments

Separate from const overloads, C++ also allows methods which differ only by the constness of parameters for some reason:

class MyClass
{
public:
    void MyFunction(void*);
    void MyFunction(const void*);
};

Discovered here in OpenCV:

https://github.com/opencv/opencv/blob/e250bae356f2a69026c8b24524fc0d768e180dbf/modules/flann/include/opencv2/flann/any.h#L48-L49

PathogenDavid avatar Jan 31 '21 01:01 PathogenDavid

Wrote a test for this issue. Even though ConstOverloadRenameTransformation is for method const-ness, I think it makes sense to someone who isn't a C++ spec nerd to expect it to handle this case as well.

[Fact]
[RelatedIssue("https://github.com/InfectedLibraries/Biohazrd/issues/154")]
public void ConstParameterOverloads()
{
    TranslatedLibrary library = CreateLibrary
    (@"
void Test(int*);
void Test(const int*);
"
    );

    Assert.Equal("Test", library.Declarations[0].Name);
    Assert.Equal("Test", library.Declarations[1].Name);
    Assert.Equal(2, library.Declarations.Count);

    TranslatedLibrary transformed = new ConstOverloadRenameTransformation().Transform(library);

    TranslatedFunction function0 = Assert.IsType<TranslatedFunction>(transformed.Declarations[0]);
    TranslatedFunction function1 = Assert.IsType<TranslatedFunction>(transformed.Declarations[1]);
    Assert.NotEqual(function0.Name, function1.Name);
}

We should also add a test case for methods (as with the example in the OP.)

We should also check for this awful and contrived but valid case:

class MyClass
{
public:
    void Test(int*);
    void Test(const int*);
    void Test(int*) const;
    void Test(const int*) const;
};

PathogenDavid avatar Feb 22 '21 05:02 PathogenDavid