超载分配运算符和零规则

发布于 2025-01-26 01:18:57 字数 378 浏览 1 评论 0原文

我已经写了一个模板类a< t>,并且正在使用零规则(我让编译器生成destructor,复制/移动构造函数和分配运算符过载)。 但是,我现在需要一个自定义的分配运算符,该操作员以不同的类型b< t>作为参数:

A<T>& operator=(const B<T>& rhs);

此实现会阻止编译器生成默认的驱动器等吗? 我的猜测是不,因为编译器生成的生成

A<T>& operator=(const A<T>& rhs);

与我要实施的过载完全不同。

I have written a template class A<T> and I am making use of the rule of zero (I let the compiler generate the destructor, copy/move constructors and assignment operator overloadings).
However, I need now a customized assignment operator that takes a different type B<T> as argument:

A<T>& operator=(const B<T>& rhs);

Will this implementation prevent the compiler from generating the default destructor etc.?
My guess is no, because the compiler generates

A<T>& operator=(const A<T>& rhs);

which is just entirely different from the overloading I want to implement.

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

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

发布评论

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

评论(1

⒈起吃苦の倖褔 2025-02-02 01:18:57

根据我的理解,添加oterator = Overload将不是防止编译器根据0的规则生成默认的编译器。

我以此为基础操作员= Overload是不是实际上a 复制分配 nor a 移动分配
因此,有关通用默认构造函数和分配运算符的规则无关。

我用MSVC进行了验证。

您可以使用下面的代码来验证编译器:

#include <iostream>

template <typename T>
struct B
{
    B(T const & n) : bn(n) {}
    T bn{ 0 };
};

template <typename T>
struct A
{
    A(T const & n) : an(n) {}
    A<T>& operator=(const B<T>& rhs)
    {
        an = rhs.bn;
        return *this;
    }
    T an{ 0 };
};

int main()
{
    A<int> a1{ 5 };
    A<int> a2{ 6 };
    std::cout << a2.an << ",";
    a2 = a1;    // Use default assinment
    std::cout << a2.an << ",";
    B<int> b{ 3 };
    a2 = b;     // Use custom assignment
    std::cout << a2.an << std::endl;
    return 0;
}

输出应为: 6,5,3

6 是值a&lt; int&gt; A2 5 构建是从a&lt; int&gt; A1 3 是从b&lt; int&gt; B

注意:替代方案是使用用户定义的转换函数,如@louisgo所述(请参见上文)。

According to my understanding, adding a operator= overload will not prevent the compiler from generating the default one according to the rule of 0.

I base this understanding on the fact that your operator= overload is not in fact a copy assignment, nor a move assignment.
Therefore the rules about generaing default constructors and assignment operators are not relevant.

I verified it with MSVC.

You can use the code below to verify with your compiler:

#include <iostream>

template <typename T>
struct B
{
    B(T const & n) : bn(n) {}
    T bn{ 0 };
};

template <typename T>
struct A
{
    A(T const & n) : an(n) {}
    A<T>& operator=(const B<T>& rhs)
    {
        an = rhs.bn;
        return *this;
    }
    T an{ 0 };
};

int main()
{
    A<int> a1{ 5 };
    A<int> a2{ 6 };
    std::cout << a2.an << ",";
    a2 = a1;    // Use default assinment
    std::cout << a2.an << ",";
    B<int> b{ 3 };
    a2 = b;     // Use custom assignment
    std::cout << a2.an << std::endl;
    return 0;
}

The output should be: 6,5,3:

6 is the value A<int> a2 is constructed with, 5 is the value assigned from A<int> a1, and 3 is the value assigned from B<int> b.

Note: an alternative would be to use a user-defined conversion function, as @LouisGo commented (see above).

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