Ambiguous method from inheritance when using a CRTP pattern

我怕爱的太早我们不能终老 提交于 2020-01-23 12:18:18

问题


I am defining a DoubleWrapper class inheriting from two CRTP base classes, Ratioable and Divable, that both define operator/(), with different signatures:

T operator/(double const& scalar) const { return T(this->underlying().get() / scalar); }
double operator/(T const& other) const { return this->underlying().get() / other.get(); }

They differ both by return type and parameter type. However compiler is complaining about operator/() being ambiguous. Note that the constructor is explicit so there is no ambiguous conversion from double to DoubleWrapper.

  • On Visual Studio 2017 it is compiling and running fine however I get a tooltip in the code "more than one operator "/" matches those operands: (...)" at the place of use. If I rename the operator to make it a regular method (divide(...)), I get a compile error :

error C2385: ambiguous access of 'divide' note: could be the 'divide' in base 'Divable' note: or could be the 'divide' in base 'Ratioable'

  • G++ 6.2 gives me a compile error even with the operator version:

    error: request for member ‘operator/’ is ambiguous double b = a / a; ^ note: candidates are: double Ratioable::operator/(const T&) const [with T = DoubleWrapper] double operator/(T const& val) const { return this->underlying().get() / val.get(); } ^~~~~~~~ note: T Divable::operator/(const double&) const [with T = DoubleWrapper] T operator/(double const& val) const { return T(this->underlying().get()/ val); } ^~~~~~~~

C++ allows methods with the same name as long as they have different signatures. Where is the ambiguity coming from ?

Test code:

    DoubleWrapper a(10.);
    double b  = a / (a/2.); // Both operator/ should be called. I would expect b value to be 2.

Source code:

    /* Curiously Recurring Template Pattern  */
template <typename T, template<typename> class crtpType>
struct crtp
{
    T& underlying() { return static_cast<T&>(*this); }
    T const& underlying() const { return static_cast<T const&>(*this); }
};

/* Inheriting class can be divided by a scalar */
template<typename T>
struct Divable : crtp<T, Divable>
{
    T operator/(double const& scalar) const { return T(this->underlying().get() / scalar); }
};

/* Inheriting class can be divided by itself */
template<typename T>
struct Ratioable : crtp<T, Ratioable>
{
    double operator/(T const& other) const { return this->underlying().get() / other.get(); }
};

struct DoubleWrapper : 
    public Divable<DoubleWrapper>, 
    public Ratioable<DoubleWrapper>
{
    explicit DoubleWrapper(double val) : val_(val) {}

    double get() const { return val_; }

private:
    double val_;
};

回答1:


Name resolution is done before overload resolution.

There are no operator/ in DoubleWrapper so the compiler goes looking for operator/ in it's base classes and finds one in both making the name ambiguous (and no overload resolution takes place).

You may resolve name resolution with using:

struct DoubleWrapper : 
    public Divable<DoubleWrapper>, 
    public Ratioable<DoubleWrapper>
{
    using Divable<DoubleWrapper>::operator/;
    using Ratioable<DoubleWrapper>::operator/;

    explicit DoubleWrapper(double val) : val_(val) {}

    double get() const { return val_; }

private:
    double val_;
};

Interesting reading:

https://en.wikipedia.org/wiki/Dominance_(C++)

http://en.cppreference.com/w/cpp/language/unqualified_lookup



来源:https://stackoverflow.com/questions/46912649/ambiguous-method-from-inheritance-when-using-a-crtp-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!