简体   繁体   English

为什么我不能用C ++中的模板版本覆盖默认的复制构造函数和赋值运算符

[英]Why can't I override the default copy constructor and assignment operator with template versions in C++

I asked this question about overloading the copy constructor and assignment operator with template versions and considering the confusion involving around the question (since it seems to be a compiler bug), I thought I'd try with only template copy constructor and template assignment operator to see what happens. 我问过这个问题,关于使用模板版本重载复制构造函数和赋值运算符,并考虑到涉及问题的混淆(因为它似乎是编译器错误),我想我只尝试使用模板复制构造函数和模板赋值运算符来走着瞧吧。 But they are completely ignored by the compiler. 但是编译器完全忽略了它们。

struct BaseClass
{
public:
  BaseClass() {}

  template<typename T>
  BaseClass(const T& a_other)
  {
    int i = 0; // for break point which is not hit
  }

  template<typename T>
  BaseClass& operator= (const T& a_other)
  {
    int i = 0; // for break point which is not hit
    return *this;
  }

};

struct MyClass : public BaseClass
{
};

int main()
{
  MyClass i, j;
  i = j;

  return 0;
}

Why can't I over-ride the defaults with template versions (I suspect the answer will be the defaults are a better match but I would like the template versions to act as defaults as well)? 为什么我不能用模板版本覆盖默认值(我怀疑答案是默认值是更好的匹配,但我希望模板版本也可以作为默认值)? And is there anything I can do to make sure the template versions are called instead of the defaults? 我能做些什么来确保调用模板版本而不是默认值?

template<typename T>
BaseClass(const T& a_other) 

First of all, this is not a copy-constructor. 首先,这不是复制构造函数。 It is rather a templated constructor. 它是一个模板化的构造函数。

The copy-constructor should be this: 复制构造函数应该是这样的:

BaseClass(const BaseClass & a_other)

Notice the difference? 请注意区别?

Note that the templated constructor doesn't define copy-constructor . 请注意, 模板化构造函数不定义复制构造函数 The compiler will still generate a default copy-constructor for you, instead of instantiating the templated constructor. 编译器仍然会生成一个默认的拷贝构造函数为你,而不是实例化的模板构造函数。

Same argument for copy-assignment. 复制赋值的参数相同。

As mentioned in answers to your other question, the standard specifically disallows it. 正如您在其他问题的答案中所提到的,该标准明确禁止它。

I'd guess that a rationale is that if a non-default for these constructors is necessary it would be because they need to deal with the specifics of the class in question. 我猜这个理由是,如果这些构造函数的非默认值是必要的,那将是因为它们需要处理所讨论的类的细节。 A 'generic' solution wouldn't make sense and might quietly hide potential problems. “通用”解决方案没有意义,可能会悄悄隐藏潜在的问题。

Some people might believe it's bad enough that there's already the 'generic' implicit versions of these functions, which silently does the wrong thing for many classes. 有些人可能认为已经存在这些函数的“通用”隐式版本已经足够糟糕了,这些函数默默地为许多类做了错误的事情。

The standardese disallowing template versino of these is here: 禁止使用这些模板的标准版本在这里:

From C++03 12.8 "Copying class objects" 来自C ++ 03 12.8“复制类对象”

  • (Para 1): A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments (8.3.6) (第1段):类X的非模板构造函数是一个复制构造函数,如果它的第一个参数是类型X&,const X&,volatile X&或const volatile X&,并且没有其他参数或者所有其他参数都有默认值论据(8.3.6)
  • (Para 9): A user-declared copy assignment operator X::operator= is a non-static non-template member function of class X with exactly one parameter of type X, X&, const X&, volatile X& or const volatile X& (第9段):用户声明的复制赋值运算符X :: operator =是类X的非静态非模板成员函数,只有一个参数类型为X,X&,const X&,volatile X&或const volatile X&

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM