joboccara / NamedType

Implementation of strong types in C++
MIT License
766 stars 85 forks source link

gcc 9.3 claims operator++ is ambiguous #61

Open usefulcat opened 3 years ago

usefulcat commented 3 years ago

I added the following test case to tests.cpp, which is just a trivial combination of the contents of the PreIncrementable and PostIncrementable tests:

TEST_CASE("PreAndPostIncrementable")
{
    using StrongInt = fluent::NamedType<int, struct StrongIntTag, fluent::PreIncrementable, fluent::PostIncrementable>;
    {
        StrongInt a{1};
        StrongInt b = ++a;
        CHECK( a.get() == 2 );
        CHECK( b.get() == 2 );
    }
    {
        StrongInt a{1};
        StrongInt b = a++;
        CHECK( a.get() == 2 );
        CHECK( b.get() == 1 );
    }
}

gcc 9.3 fails to compile it, saying "error: request for member ‘operator++’ is ambiguous".

I thought I would try fixing it myself but so far I haven't been able to figure out why it doesn't work, let alone how to fix it. This problem also affects any type that uses Arithmetic, which is how I originally discovered it.

Nicholas42 commented 2 years ago

Ran into the same problem. I think this is a bug in gcc. I just opened a question stackoverflow regarding this: https://stackoverflow.com/questions/69540048/gcc-ambiguous-inherited-increment

I don't think that the hot-fix (using the operators) will work in general for NamedType, since it would have to know if it is pre- and postincrementable. But adding the following lines to struct Arithmetic fixes it for arithmetic types at least:

    using PostIncrementable<T>::operator++;
    using PreIncrementable<T>::operator++;
    using PostDecrementable<T>::operator--;
    using PreDecrementable<T>::operator--;

I will add a pull request for this.