C++11中"= delete;"的使用

C++11中,对于deleted函数,编译器会对其禁用,从而避免某些非法的函数调用或者类型转换,从而提高代码的安全性。

对于 C++ 的类,如果程序员没有为其定义特殊成员函数,那么在需要用到某个特殊成员函数的时候,编译器会隐式的自动生成一个默认的特殊成员函数,比如默认的构造函数、析构函数、拷贝构造函数以及拷贝赋值运算符。

为了能够让程序员显式的禁用某个函数,C++11标准引入了一个新特性:deleted函数。程序员只需在函数声明后加上”=delete;”,就可将该函数禁用

deleted函数特性还可用于禁用类的某些转换构造函数,从而避免不期望的类型转换。

deleted函数特性还可以用来禁用某些用户自定义的类的new操作符,从而避免在自由存储区创建类的对象。

必须在函数第一次声明的时候将其声明为deleted函数,否则编译器会报错。即对于类的成员函数而言,deleted函数必须在类体里(inline)定义,而不能在类体外(out-of-line)定义。

虽然defaulted函数特性规定了只有类的特殊成员函数才能被声明为defaulted函数,但是deleted函数特性并没有此限制。非类的成员函数,即普通函数也可以被声明为deleted函数

In C++11, defaulted and deleted functions give you explicit control over whether the special member functions are automatically generated.

The opposite of a defaulted function is a deleted function.

Deleted functions are useful for preventing object copying, among the rest. Recall that C++ automatically declares a copy constructor and an assignment operator for classes. To disable copying, declare these two special member functions “=delete”.

The delete specifier can also be used to make sure member functions with particular parameters aren’t called.

=default: it means that you want to use the compiler-generated version of that function, so you don't need to specify a body.

=delete: it means that you don't want the compiler to generate that function automatically.

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

#include "delete.hpp"
#include <iostream>

//
// reference: http://www.learncpp.com/cpp-tutorial/b-6-new-virtual-function-controls-override-final-default-and-delete/
class Foo
{
	Foo& operator=(const Foo&) = delete; // disallow use of assignment operator
	Foo(const Foo&) = delete; // disallow copy construction
};

class Foo_1
{
	Foo_1(long long); // Can create Foo() with a long long
	Foo_1(long) = delete; // But can't create it with anything smaller
};

class Foo_2
{
	Foo_2(long long); // Can create Foo() with a long long
	template<typename T> Foo_2(T) = delete; // But can't create it with anything else
};

///
// reference: http://www.bogotobogo.com/cplusplus/C11/C11_default_delete_specifier.php
class A_
{
public:
	A_(int a){};
	A_(double) = delete;         // conversion disabled
	A_& operator=(const A_&) = delete;  // assignment operator disabled
};

int test_delete1()
{
	A_ a_(10);     // OK
	// A_ b(3.14);   // Error: conversion from double to int disabled
	// a = b;       // Error: assignment operator disabled

	return 0;
}


// reference: https://msdn.microsoft.com/zh-cn/library/dn457344.aspx
struct widget
{
	// deleted operator new prevents widget from being dynamically allocated.
	void* operator new(std::size_t) = delete;
};

GitHubhttps://github.com/fengbingchun/Messy_Test

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值