未调用复制构造函数,但编译器抱怨没有

发布于 2024-08-16 02:22:31 字数 1341 浏览 5 评论 0原文

给出以下代码:

#include <boost/noncopyable.hpp>

enum Error { ERR_OK=0 };

struct Filter : private boost::noncopyable
{
  Filter() {}
  virtual ~Filter() {}

  virtual int filter(int* data) const = 0;

};

struct  SpecialFilter : public Filter, private boost::noncopyable
{
  inline SpecialFilter(unsigned int min, unsigned int max) : min(min), max(max) {}
  virtual ~SpecialFilter() {}

  virtual int filter(int* data) const
  {
    // ...
    return ERR_OK;
  }

  unsigned int min;
  unsigned int max;
};

struct AClass
{
  AClass() {}
  AClass(const AClass& other) {}
  ~AClass() {}

  int specialFilter(int channel, int minThreshold, int maxThreshold)
  {
    // ...
    return filter(channel, SpecialFilter(123, 321));
  }

  int filter(int channel, const Filter& filter)
  {
    // ...
    return ERR_OK;
  }

};

我的编译器(GCC 4.2)抱怨:

- warning: direct base ‘boost::noncopyable_::noncopyable’ inaccessible in ‘SpecialFilter’ due to ambiguity
- noncopyable.hpp: In copy constructor ‘Filter::Filter(const Filter&)’:
- noncopyable.hpp:27: error: ‘boost::noncopyable_::noncopyable::noncopyable(const boost::noncopyable_::noncopyable&)’ is private
- synthezised method first required here: [return filter(channel, SpecialFilter(123, 321));]

但我不调用复制构造函数!

Given the following code:

#include <boost/noncopyable.hpp>

enum Error { ERR_OK=0 };

struct Filter : private boost::noncopyable
{
  Filter() {}
  virtual ~Filter() {}

  virtual int filter(int* data) const = 0;

};

struct  SpecialFilter : public Filter, private boost::noncopyable
{
  inline SpecialFilter(unsigned int min, unsigned int max) : min(min), max(max) {}
  virtual ~SpecialFilter() {}

  virtual int filter(int* data) const
  {
    // ...
    return ERR_OK;
  }

  unsigned int min;
  unsigned int max;
};

struct AClass
{
  AClass() {}
  AClass(const AClass& other) {}
  ~AClass() {}

  int specialFilter(int channel, int minThreshold, int maxThreshold)
  {
    // ...
    return filter(channel, SpecialFilter(123, 321));
  }

  int filter(int channel, const Filter& filter)
  {
    // ...
    return ERR_OK;
  }

};

My compiler (GCC 4.2) complains:

- warning: direct base ‘boost::noncopyable_::noncopyable’ inaccessible in ‘SpecialFilter’ due to ambiguity
- noncopyable.hpp: In copy constructor ‘Filter::Filter(const Filter&)’:
- noncopyable.hpp:27: error: ‘boost::noncopyable_::noncopyable::noncopyable(const boost::noncopyable_::noncopyable&)’ is private
- synthezised method first required here: [return filter(channel, SpecialFilter(123, 321));]

But I don't call the copy constructor!

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

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

发布评论

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

评论(3

时光无声 2024-08-23 02:22:31

你永远不会调用复制构造函数。编译器总是隐式地为您调用复制构造函数。因此,您需要学会识别可能被调用的情况。

当您将 const 引用附加到临时对象时,

...
return filter(channel, SpecialFilter(123, 321));
...

编译器有权执行临时对象的副本,并需要可访问的复制构造函数(即使实际上不会调用它)。这就是导致您的情况出现问题的原因。

换句话说,当您使某种类型不可复制时,您也就放弃了将 const 引用附加到该类型的临时对象的可能性。

You never call copy constructor. The copy constructor is always called for you implicitly by the compiler. So you need to learn to recognize situations when it might be called.

When you attach a const reference to a temporary object

...
return filter(channel, SpecialFilter(123, 321));
...

the compiler has the right to perform a copy of the temporary object and require an accessible copy constructor (even if it won't be actually called). This is what is causing the problem in your case.

In other words, when you make some type non-copyable, you also give up the possibility to attach const references to temporary objects of that type.

眼眸里的那抹悲凉 2024-08-23 02:22:31

首先,从 SpecialFilter 中删除私有派生 - 这是没有必要的,因为 Filter 已经不可复制。像这样的问题就是为什么我认为像 boost::non_copyable 这样的解决方案是一个坏主意 - 有更简单的方法来表示你不需要副本。

其次,虽然我不确定这是你的问题,但 C++ 表示在多种情况下编译器必须可以使用公共复制构造函数,即使编译器实际上并不使用它

Firstly, remove the private derivation from SpecialFilter - it is not necessary, as Filter is already not copyable. Problems like this are why I think solutions like boost::non_copyable are a bad idea - there are simpler ways of saying you don't want copies.

Secondly, though I'm not sure this is your problem, C++ says that a public copy constructor must be available to the compiler under several circimstances, even if the compiler does not actually use it.

Bonjour°[大白 2024-08-23 02:22:31

请记住,当您按值传递对象并返回对象时 -->复制构造函数被调用。

Remember when you pass object and return object by value --> copy constructor is invoked.

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