使用C ++ 11中显式删除的成员函数,从不可复制的基类继承仍然值得吗? [英] With explicitly deleted member functions in C++11, is it still worthwhile to inherit from a noncopyable base class?

查看:88
本文介绍了使用C ++ 11中显式删除的成员函数,从不可复制的基类继承仍然值得吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C ++ 11中明确删除的成员函数,仍然值得从不可复制的基类继承吗?

With explicitly deleted member functions in C++11, is it still worthwhile to inherit from a noncopyable base class?

我在说的是,私有地继承具有私有或已删除副本构造函数和副本分配的基类(例如 boost :: noncopyable )。

I'm talking about the trick where you privately inherit a base class which has private or deleted copy constructor and copy assignment (e.g. boost::noncopyable).

此<一个href = https://stackoverflow.com/questions/7823990/what-are-the-advantages-of-boostnoncopyable>问题仍然适用于C ++ 11吗?

Are the advantages put forward in this question still applicable to C++11?

我不明白为什么有人认为使类在C ++ 11中不可复制更容易。

I don't get why some people claim it's easier to make a class non-copyable in C++11.

在C ++ 03中:

private:
    MyClass(const MyClass&) {}
    MyClass& operator=(const MyClass&) {}

在C ++ 11中:

MyClass(const MyClass&) = delete;
MyClass& operator=(const MyClass&) = delete;






编辑:

正如许多人指出的那样,为私有副本构造函数和副本赋值运算符提供空主体(即{})是错误的,因为那样会允许该类本身会调用这些运算符而不会出现任何错误。首先,我没有添加{},但是遇到了一些链接器问题,出于某些愚蠢的原因,我添加了{}(我不考虑这种情况)。我知道更好。 :-)

As many people have pointed out, it was a mistake to provide empty bodies (i.e. {}) for the private copy constructor and copy assignment operator, because that would allow the class itself invoke those operators without any errors. I first started out not adding the {}, but ran into some linker issues that made me add the {} for some silly reason (I don't remeber the circumstances). I know better know. :-)

推荐答案

嗯,这:

private:
    MyClass(const MyClass&) {}
    MyClass& operator=(const MyClass&) {}

从技术上讲,仍然允许 MyClass 由成员和朋友复制。当然,从理论上讲,这些类型和函数都在您的控制之下,但是该类仍然是可复制的。至少使用 boost :: noncopyable = delete 没人可以复制该类。

Still technically allows MyClass to be copied by members and friends. Sure, those types and functions are theoretically under your control, but the class is still copyable. At least with boost::noncopyable and = delete, nobody can copy the class.


我不明白为什么有些人声称让上课变得更容易-可在C ++ 11中复制。

I don't get why some people claim it's easier to make a class non-copyable in C++11.

它不是更容易而是更易于消化。

It's not so much "easier" as "more easily digestible".

请考虑以下问题:

class MyClass
{
private:
    MyClass(const MyClass&) {}
    MyClass& operator=(const MyClass&) {}
};

如果您是一位C ++程序员,已经阅读了C ++的介绍性文字,但很少接触惯用语言C ++(即C ++程序员的很多),这令人困惑。它声明了复制构造函数和复制赋值运算符,但它们为空。那么为什么要声明它们呢?是的,它们是私有,但这只会引发更多问题:为什么将它们设为私有?

If you are a C++ programmer who has read an introductory text on C++, but has little exposure to idiomatic C++ (ie: a lot of C++ programmers), this is... confusing. It declares copy constructors and copy assignment operators, but they're empty. So why declare them at all? Yes, they're private, but that only raises more questions: why make them private?

要了解为什么这样做会阻止复制,您必须意识到,通过将它们声明为私有,可以将其设置为非成员/朋友无法复制。对于新手来说,这不是立即显而易见的。他们尝试复制它时也不会收到错误消息。

To understand why this prevents copying, you have to realize that by declaring them private, you make it so that non-members/friends cannot copy it. This is not immediately obvious to the novice. Nor is the error message that they will get when they try to copy it.

现在,将其与C ++ 11版本进行比较:

Now, compare it to the C++11 version:

class MyClass
{
public:
    MyClass(const MyClass&) = delete;
    MyClass& operator=(const MyClass&) = delete;
};

了解此类不能被复制需要做什么?只是理解 = delete 语法的含义。任何解释C ++ 11语法规则的书都会告诉您确切的功能。对于没有经验的C ++用户,此代码的效果是显而易见的。

What does it take to understand that this class cannot be copied? Nothing more than understanding what the = delete syntax means. Any book explaining the syntax rules of C++11 will tell you exactly what that does. The effect of this code is obvious to the inexperienced C++ user.

这个惯用法的最大优点是它成为惯用法,因为它是最清晰,最明显的表达方式

What's great about this idiom is that it becomes an idiom because it is the clearest, most obvious way to say exactly what you mean.

即使 boost :: noncopyable 也需要更多的思考。是的,它被称为不可复制,因此可以自我记录。但是,如果您以前从未看过它,则会引发疑问。为什么从无法复制的内容中衍生出东西?为什么我的错误消息中提到 boost :: noncopyable 的副本构造函数?再说一次,理解习语需要更多的精神努力。

Even boost::noncopyable requires a bit more thought. Yes, it's called "noncopyable", so it is self-documenting. But if you've never seen it before, it raises questions. Why are you deriving from something that can't be copied? Why do my error messages talk about boost::noncopyable's copy constructor? Etc. Again, understanding the idiom requires more mental effort.

这篇关于使用C ++ 11中显式删除的成员函数,从不可复制的基类继承仍然值得吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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