带有运算符的好奇重复模板

发布于 2024-10-31 01:13:12 字数 874 浏览 0 评论 0原文

我可以在 G++ 上运行,但在 Visual Studo 2008 上无法编译。

template<typename T, typename DerivedT >
struct Foo
{    
    template<typename Scale>
    DerivedT operator * (const Scale i)
    {
     DerivedT result;
     return result;
    }
};

template<typename T>
struct Bar : public Foo<T, Bar<T> >
{   
    // Removing this operator gets rid of the error.
    Bar& operator * (const Bar& boo)
    {
     return *this;
    }
};

int main()
{
    Bar<float> bar;
    bar = bar * 3;

    return 0;
}

我收到错误

Error   1   error C2679: binary '*' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)

即使我将 Foo 运算符显式定义为 int/double/float ,它也会返回相同的错误消息。有什么办法可以克服这个吗?

编辑: 仅当派生类重载也在基类中定义的运算符 * 时,这种情况才会崩溃。

I have this working on G++ but on Visual Studo 2008 this will not compile.

template<typename T, typename DerivedT >
struct Foo
{    
    template<typename Scale>
    DerivedT operator * (const Scale i)
    {
     DerivedT result;
     return result;
    }
};

template<typename T>
struct Bar : public Foo<T, Bar<T> >
{   
    // Removing this operator gets rid of the error.
    Bar& operator * (const Bar& boo)
    {
     return *this;
    }
};

int main()
{
    Bar<float> bar;
    bar = bar * 3;

    return 0;
}

I get the error

Error   1   error C2679: binary '*' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)

Even if I define the Foo operator as an int/double/float explicitly it returns the same error message. Is there any way of getting past this?

EDIT:
This only falls apart when the derived class overload the operator * that is also defined in the Base class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玉环 2024-11-07 01:13:12

我不知道你是如何通过 g++ 编译这个的(我实际上对此表示怀疑),但你的代码确实由于相当明显的原因无法编译。您的 Bar 类仅公开一个运算符 *

Bar& operator * (const Bar& boo)

,并且该运算符需要一个 Bar 对象作为右侧大小的操作数。 3 不起作用,3 不是 Bar 并且不能转换为 Bar

基类的运算符 * 可能在这里起作用,但它被派生类的运算符隐藏了。这就是为什么,正如人们所期望的,删除派生类的operator * 可以消除错误。

您只需将 添加

using Foo<T, Bar<T> >::operator *;

Bar 的定义中即可取消隐藏基类的运算符,并且它应该可以编译。

I don't know how you managed to compile this by g++ (and I actually doubt that), but your code is indeed not compilable for rather obvious reasons. Your Bar class exposes only one operator *

Bar& operator * (const Bar& boo)

and that operator expects a Bar object as a right-hand size operand. 3 will not work, 3 is not Bar and is not convertible to Bar.

The base class's operator * is the one that might have worked here, but it is hidden by the derived class's operator. This is why, as one would expect, removing the derived class's operator * gets rid of the error.

You can simply add the

using Foo<T, Bar<T> >::operator *;

to the definition of Bar to unhide the base class's operator and it should compile.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文