= delete的一些用途是什么? [英] What are some uses for =delete?
问题描述
今天上午,我问了一个问题导致另一个:我应该什么时候使用 = delete
?我不认为有专门的专门针对 = delete
在SO,所以我查了一本书叫C ++编程语言。
如果还有更多话要说,或者我错了,请评论或回答。
<
= delete
是非常有用的!这里有几个例子: 基本上我们可以防止复制基类,因为它可能经常导致切片:
struct Base {
Base(){}
Base& operator =(const Base&)= delete; // disallow copying
Base(const Base&)= delete;
基本& operator =(Base&&)= delete; // disallow moving
Base(Base&&)= delete;
};
struct Der:public Base {};
void func(){
Der d;
Base base = d; //这将不工作,因为复制构造函数被删除!
//这种行为是需要的 - 否则会发生切片
}
当模板函数无法以某种类型运行时,它也很有用:
template< class T>
void fn(T p){/ * ... * /}; // do something with T
void fn(int)= delete; //不允许使用int
void fun(){
fn(4); //啊哈!不能使用fn()与int!
fn(-4.5); // fine
fn(hello); // fine
}
= delete
也可以禁止不必要的转换:
struct Z {
Z(double); //可以用double
初始化Z(int)= delete; //但不是整数
};
void f(){
Z z1 {1}; //错误!不能使用int
Z z2 {1.0}; // double is ok
}
$ b b
= delete
的一些更高级用途包括禁止堆栈或免费商店分配:
class FS_Only {
〜FS_Only()= delete; // disallow stack allocation
};
类Stack_Only {
void * operator new(size_t)= delete; // disallow heap allocation
};
...你的想法。希望这有助于某人! = delete
可以帮助编写可读,无bug和优雅的代码。
编辑:
正如在注释中正确指出的,现在不可能删除 FS_Only
所以这一个不是很好地使用 = delete
毕竟。
Earlier today I asked a question that led to another one: When should I use =delete
? I don't think there is a post dedicated solely to =delete
on SO, so I looked it up in a book called "The C++ Programming Language". I will list my findings in my answer below.
Please comment or answer if there's more to say or if I'm mistaken.
It turns out that =delete
is extremely useful! Here are a few examples:
Basically we can prevent copying base classes because it might often lead to slicing:
struct Base {
Base(){}
Base& operator=(const Base&) = delete; // disallow copying
Base(const Base&) = delete;
Base& operator=(Base && ) = delete; // disallow moving
Base(Base && ) = delete;
};
struct Der : public Base {};
void func() {
Der d;
Base base = d; // this won't work because the copy constructor is deleted!
// this behavior is desired - otherwise slicing would occur
}
It's also useful when a template function cannot run with a certain type:
template<class T>
void fn(T p) { /* ... */ }; // do something with T
void fn(int) = delete; // disallow use with int
void fun() {
fn(4); // aha! cannot use fn() with int!
fn(-4.5); // fine
fn("hello");// fine
}
=delete
can also disallow undesired conversions:
struct Z {
Z(double); // can initialize with a double
Z(int) = delete; // but not with an integer
};
void f() {
Z z1 { 1 }; // error! can't use int
Z z2 { 1.0 }; // double is ok
}
Some more advanced uses of =delete
include prohibiting stack or free store allocation:
class FS_Only {
~FS_Only() = delete; // disallow stack allocation
};
class Stack_Only {
void* operator new(size_t) = delete; // disallow heap allocation
};
... You get the idea. Hope this helps someone! =delete
can help write readable, bugless and elegant code.
Edit:
As it was correctly pointed out in the comments, it is now impossible to delete FS_Only
objects, so this one isn't such a good use of =delete
after all.
这篇关于= delete的一些用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!