在C+11中,对于defaulted函数,编译器会为其自动生成默认的函数定义体,从而获得更高的代码执行效率,也可免除程序员手动定义该函数的工作量。
C++的类有四类特殊成员函数,它们分别是:默认构造函数、析构函数、拷贝构造函数以及拷贝赋值运算符。这些类的特殊成员函数负责创建、初始化、销毁,或者拷贝类的对象。如果程序员没有显式地为一个类定义某个特殊成员函数,而又需要用到该特殊成员函数时,则编译器会隐式的为这个类生成一个默认的特殊成员函数。当存在用户自定义的特殊成员函数时,编译器将不会隐式的自动生成默认特殊成员函数,而需要程序员手动编写,加大了程序员的工作量。并且手动编写的特殊成员函数的代码执行效率比编译器自动生成的特殊成员函数低。
C++11标准引入了一个新特性:defaulted函数。程序员只需在函数声明后加上”=default;”,就可将该函数声明为defaulted函数,编译器将为显式声明的defaulted函数自动生成函数体。
defaulted函数特性仅适用于类的特殊成员函数,且该特殊成员函数没有默认参数。
defaulted函数既可以在类体里(inline)定义,也可以在类体外(out-of-line)定义。
In C++11, defaulted and deleted functions give you explicit control over whether the special member functions are automatically generated.
“=default” instructs the compiler to generate the default implementation for the function. Defaulted functions have two advantages: They are more efficient than manual implementations, and they rid the programmer from the chore of defining those functions manually.
By default, C++ will provide a default constructor, copy constructor, copy assignment operator (operator=) and a destructor. If you provide alternate versions of any of these functions for your class, C++ will not provide a default version. However, in C++11, you can now specify that you would like the compiler to provide a default one anyway. This is done by prototyping the function and using the default specifier.
The default specifier can only be used with functions that have a default.
=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 "default.hpp"
#include <iostream>
////////////////////////////////////////////////
// reference: http://www.learncpp.com/cpp-tutorial/b-6-new-virtual-function-controls-override-final-default-and-delete/
class Foo
{
Foo(int x); // Custom constructor
Foo() = default; // The compiler will now provide a default constructor for class Foo as well
};
/////////////////////////////////////////////////
// reference: http://en.cppreference.com/w/cpp/language/default_constructor
struct A
{
int x;
A(int x = 1) : x(x) {} // user-defined default constructor
};
struct B : A
{
// B::B() is implicitly-defined, calls A::A()
};
struct C
{
A a;
// C::C() is implicitly-defined, calls A::A()
};
struct D : A
{
D(int y) : A(y) {}
// D::D() is not declared because another constructor exists
};
struct E : A
{
E(int y) : A(y) {}
E() = default; // explicitly defaulted, calls A::A()
};
struct F
{
int& ref; // reference member
const int c; // const member
// F::F() is implicitly defined as deleted
};
int test_default1()
{
A a;
B b;
C c;
// D d; // compile error
E e;
// F f; // compile error
return 0;
}
///////////////////////////////////////////////////////
// reference: https://msdn.microsoft.com/zh-cn/library/dn457344.aspx
struct widget
{
widget() = default;
inline widget& operator=(const widget&);
};
// Notice that you can default a special member function outside the body of a class as long as it’s inlinable.
inline widget& widget::operator=(const widget&) = default;