=default關(guān)鍵字

=default關(guān)鍵字使用的地方

The "default" mechanism can be used for any function that has a default. C++11FAQ

也就是說(shuō)下面這6個(gè)函數(shù)都可以跟著=default關(guān)鍵字.

class A{
public:
    A() = default; // Default constructor
    A(const A&) = default; // Copy constructor
    A(A&&) = default; // Move constructor(since C++11)
    A& operator=(const A&) = default; // Copy assignment operator
    A& operator=(A&&) = default; // Move assignment operator (since C++11)
    ~A() = default; // Destructor
};

PS: VS2013暫時(shí)不支持Move constructorMove assignment operator使用=default關(guān)鍵字.

For defaulted functions, using =default to request memberwise move constructors and move assignment operators is not supported. New In VS2013

=default關(guān)鍵字的作用:顯式的要求編譯器生成函數(shù)的一個(gè)默認(rèn)版本。

比如說(shuō)下面的代碼,如果用戶寫(xiě)了一個(gè)構(gòu)造函數(shù)A(int){};,那么就不會(huì)為class A生成一個(gè)默認(rèn)的構(gòu)造函數(shù)。這個(gè)時(shí)候如果用戶還需要一個(gè)和編譯器自動(dòng)生成的一模一樣的默認(rèn)的構(gòu)造函數(shù),那么就可以使用=default關(guān)鍵字,顯式的讓編譯器生成一個(gè)。

class A{
public:
    A(int){};
    A() = default;
    //A(){}; // old way to get an empty constructors like default one.
};

按照Bjarne的說(shuō)法,=default的好處在于。這些好處在default constructor上效果不大,但是在Move constructor和Move assignment operator上好處比較大了,因?yàn)檫@兩個(gè)函數(shù)都比較難實(shí)現(xiàn)。

Leaving it to the compiler to implement the default behavior is simpler, less error-prone, and often leads to better object code. C++11FAQ

寫(xiě)一個(gè)=default和寫(xiě)一個(gè)空的構(gòu)造函數(shù)有什么區(qū)別?

從執(zhí)行效果上,=default和一個(gè)空的構(gòu)造函數(shù)沒(méi)有什么區(qū)別,都是什么都不做。但是一旦某個(gè)類有了一個(gè)用戶定義的構(gòu)造函數(shù),那這個(gè)類就不再是aggregate類型,和不再是trivial類型,和不再是POD類型了。
比如說(shuō)下面這段拷貝至[StackOverflow]的代碼所演示的。

#include <type_traits>
struct X {
    X() = default;
};

struct Y {
    Y() {};
};

int main() {
    static_assert(std::is_trivial<X>::value, "X should be trivial");
    static_assert(std::is_pod<X>::value, "X should be POD");

    static_assert(!std::is_trivial<Y>::value, "Y should not be trivial");
    static_assert(!std::is_pod<Y>::value, "Y should not be POD");
}

另外的不同是,使用=default定義的函數(shù),其constexprexception specification保證了和編譯器生成的函數(shù)是一致的。而如果要手寫(xiě)的話,就要顯式的把constexprexception specification的信息加上。就如下面的代碼所示,直接寫(xiě)的A(){}不帶有constexpr屬性,會(huì)導(dǎo)致編譯錯(cuò)誤。而struct C顯式的加上了constexpr就沒(méi)有編譯錯(cuò)誤了。
PS:這代碼在VS3013下面沒(méi)效果,最好用其他編譯器來(lái)驗(yàn)證。

struct A{
    A(){}; 
};
struct B{
    B() = default;
};
struct C{
    constexpr C(){};
};

constexpr int f(A s){ return 0; } // compiling error here
constexpr int f(B s){ return 0; }
constexpr int f(C s){ return 0; }

寫(xiě)一個(gè)=default和直接不寫(xiě)構(gòu)造函數(shù)有什么區(qū)別。

第一眼看到=default的感覺(jué)就是,這是多余的嘛,和直接不寫(xiě)也沒(méi)有區(qū)別嘛。后來(lái)想了下,在下面的這種情況下,=default還是有用處的。

class A{   
public:
    static A* CreateA() { return new A; }
private:
    A() = default;
};

在這個(gè)類中,我們希望使用類的某個(gè)默認(rèn)函數(shù),但是需要控制這個(gè)函數(shù)的訪問(wèn)權(quán)限。 在這里的代碼使用默認(rèn)的構(gòu)造函數(shù)為例子。這個(gè)時(shí)候=default就有用了,如果什么都不寫(xiě)的話,這些默認(rèn)函數(shù)的作用域是public。使用=default可以很方面的想編譯器在public作用域下生成這個(gè)函數(shù)。在沒(méi)有=defaultC++03時(shí)代,處理這種問(wèn)題,就需要程序員自己寫(xiě)個(gè)完整的實(shí)習(xí)出來(lái),對(duì)于默認(rèn)構(gòu)造函數(shù)來(lái)說(shuō),還比較簡(jiǎn)單,如果對(duì)于寫(xiě)個(gè)Copy assignment operator,還是不小的工作量的,也容易出錯(cuò)。
[StackOverflow]:http://stackoverflow.com/questions/20828907/the-new-keyword-default-in-c11

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容