C++11でのdefaultの使用

3422 ワード

C+11でdefaulted関数の場合、コンパイラはデフォルトの関数定義体を自動的に生成し、より高いコード実行効率を得ることができます.また、プログラマが手動で関数を定義する作業量を免除することもできます.
C++のクラスには、デフォルトのコンストラクション関数、解析関数、コピーコンストラクション関数、コピー付与演算子の4つの特殊なメンバー関数があります.これらのクラスの特殊なメンバー関数は、クラスのオブジェクトの作成、初期化、破棄、またはコピーを担当します.プログラマがクラスに特殊なメンバー関数を明示的に定義せず、特殊なメンバー関数を使用する必要がある場合、コンパイラはクラスにデフォルトの特殊なメンバー関数を暗黙的に生成します.ユーザーがカスタマイズした特殊なメンバー関数がある場合、コンパイラはデフォルトの特殊なメンバー関数を暗黙的に自動的に生成しません.プログラマが手動で作成する必要があり、プログラマの作業量が増加します.また,手動で記述した特殊メンバー関数のコード実行効率は,コンパイラが自動的に生成した特殊メンバー関数よりも低い.
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 


// 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;

GitHub:https://github.com/fengbingchun/Messy_Test