模板赋值运算符不会替换默认赋值运算符 [英] Template assignment operator doesn't replace the default assignment operator

查看:70
本文介绍了模板赋值运算符不会替换默认赋值运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++模板中的

完整指南第5.3节成员模板" 中:

请注意,模板赋值运算符不会替代默认值赋值运算符.对于相同类型的堆栈分配,仍会调用默认的赋值运算符.

Note that a template assignment operator doesn't replace the default assignment operator. For assignments of stacks of the same type, the default assignment operator is still called.

这是正确的吗,因为当我运行以下代码时:

Is this correct, because when I ran below code:

#include<iostream>
using namespace std;

template<typename T>
class Pair
{
    public:
            T pair1,pair2;
            Pair(T i,T j):pair1(i),pair2(j){}
            template<typename T1>Pair<T>& operator=(Pair<T1>&);             
};

template<typename T>
template<typename T1>
Pair<T>& Pair<T>::operator=(Pair<T1>& temp)
{

    this->pair1 =temp.pair1*10;//At this point
    this->pair2=temp.pair2;
    return *this;
}

int main()
{

    Pair<int>P1(10,20);
    Pair<int>P2(1,2);
    P2=P1;
    cout<<P2.pair1<<' '<<P2.pair2<<endl;
    return 1;
}

我得到了100 20的答案.

I got answer 100 20.

它没有给出默认的作业答案.

It didn't give the default assignment answer.

C ++模板《完整指南》 中的输入错误吗?

C ++模板:完整指南,作者David Vandevoorde,NicolaiM.乔苏蒂斯

C++ Templates: The Complete Guide By David Vandevoorde, Nicolai M. Josuttis

发布者:Addison Wesley

Publisher : Addison Wesley

发布日期:2002年11月12日ISBN:0-201-73484-2页数:552

Pub Date : November 12, 2002 ISBN : 0-201-73484-2 Pages : 552

推荐答案

副本分配运算符确实是由隐式声明并由重载解析考虑的.

The copy assignment operator is indeed implicitly declared and considered by overload resolution.

用户声明的副本分配运算符 X :: operator = 是类 X [..]的非静态非模板成员函数.

如果类定义未明确声明一个副本赋值运算符,一个被隐式声明 .[..]类 X 的隐式声明的副本分配运算符将填写表格

A user-declared copy assignment operator X::operator= is a non-static non-template member function of class X [..].

If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly. [..] The implicitly-declared copy assignment operator for a class X will have the form

X& X::operator=(const X&)

如果

  • 每个 X 的直接基类 B 都有一个副本赋值运算符,其参数的类型为 const B& const volatileB& B
  • 对于所有类型为 M (或其数组)的 X 的所有非静态数据成员,
  • 参数类型为 const M& const volatile M& 的运算符或 M .
  • each direct base class B of X has a copy assignment operator whose parameter is of type const B&, const volatile B& or B, and
  • for all the non-static data members of X that are of a class type M (or array thereof), each such class type has a copy assignment operator whose parameter is of type const M&, const volatile M& or M.

否则,[..]

如您所见, Pair< int> 的隐式声明的副本分配运算符具有一个类型为 Pair< int>的参数.const& -特别注意 const !如果两者都可以绑定到参数[over.ics.rank]/3:

As you can see the implicitly-declared copy assignment operator for Pair<int> has one parameter of type Pair<int> const& - note the const in particular! Overload resolution favours non-const references over const ones if both can be bound to the argument, [over.ics.rank]/3:

两个相同形式的隐式转换序列是不可区分的转换序列,除非以下情况之一规则适用:

Two implicit conversion sequences of the same form are indistinguishable conversion sequences unless one of the following rules applies:

—标准转换序列 S1 是比标准转换序列更好的转换序列 S2 如果

— Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if

  • [..]
  • S1 S2 是引用绑定(8.5.3),以及除了顶级cv限定词外,引用refer是相同的类型,,由 S2 初始化的引用所引用的类型更多cv限定,而不是由 S1 初始化的引用的类型引用.
  • [..]
  • S1 and S2 are reference bindings (8.5.3), and the types to which the references refer are the same type except for top-level cv-qualifiers, and the type to which the reference initialized by S2 refers is more cv-qualified than the type to which the reference initialized by S1 refers.

模板的专业化在参考参数中缺少 const ,因此它是一个更好的匹配并被选择.

The specialization of the template lacks a const in the reference parameter, thus it's a better match and is selected.

这篇关于模板赋值运算符不会替换默认赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆