溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++怎么使用{}?初始化器語法

發(fā)布時(shí)間:2021-11-26 14:40:30 來源:億速云 閱讀:186 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容介紹了“C++怎么使用{}初始化器語法”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

ES.23:優(yōu)先使用{}初始化器語法

Reason(原因)

優(yōu)先使用{}。{}初始化器原則簡單,更通用,更少歧義,并且比其他形式的初始化更安全。

只在你確定不會(huì)發(fā)生窄化時(shí)使用=。對(duì)于內(nèi)置算數(shù)類型,只在給auto賦值時(shí)使用=。避免()初始化,它允許模糊解析.

Example(示例)

int x {f(99)};
int y = x;
vector<int> v = {1, 2, 3, 4, 5, 6};
Exception(例外)

For containers, there is a tradition for using {...} for a list of elements and (...) for sizes:

對(duì)于容器來講,習(xí)慣上使用{...}表示要素列表,使用()表示大小。

vector<int> v1(10);    // vector of 10 elements with the default value 0
vector<int> v2{10};    // vector of 1 element with the value 10

vector<int> v3(1, 2);  // vector of 1 element with the value 2
vector<int> v4{1, 2};  // vector of 2 element with the values 1 and 2
Note(注意)

{}初始化器不允許窄化轉(zhuǎn)換(這通常是好事)并且允許顯式構(gòu)造函數(shù)(這沒有問題,我們就是要初始化一個(gè)新變量)

Example(示例)
int x {7.9};   // error: narrowing
int y = 7.9;   // OK: y becomes 7. Hope for a compiler warning
int z = gsl::narrow_cast<int>(7.9);  // OK: you asked for it
Note(注意)

{}初始化器差不多可以被用于任何初始化;其他形式的初始化則不行。

auto p = new vector<int> {1, 2, 3, 4, 5};   // initialized vector
D::D(int a, int b) :m{a, b} {   // member initializer (e.g., m might be a pair)
   // ...
};
X var {};   // initialize var to be empty
struct S {
   int m {7};   // default initializer for a member
   // ...
};

由于這個(gè)原因,{}初始化經(jīng)常被稱為“統(tǒng)一初始化”(雖然很不幸還存在很少的例外。)

Note(注意)

用一個(gè)單值初始化一個(gè)用auto聲明的變量,例如:{v},在C++17之前會(huì)產(chǎn)生以外的結(jié)果,C++17原則某種程度上好一些:

auto x1 {7};        // x1 is an int with the value 7
auto x2 = {7};      // x2 is an initializer_list<int> with an element 7

auto x11 {7, 8};    // error: two initializers
auto x22 = {7, 8};  // x22 is an initializer_list<int> with elements 7 and 8

如果你確實(shí)想要一個(gè)列表初始化,使用={...};

auto fib10 = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55};   // fib10 is a list
Note(注意)

={} 提供拷貝初始化,但是{}提供直接初始化。就像拷貝初始化和直接初始化之間的區(qū)別一樣,這會(huì)使人驚訝。{}接受顯式構(gòu)造函數(shù),={}不會(huì)。例如:

struct Z { explicit Z() {} };

Z z1{};     // OK: direct initialization, so we use explicit constructor
Z z2 = {};  // error: copy initialization, so we cannot use the explicit constructor

使用直接的{}初始化,除非你就是想禁止顯式構(gòu)造函數(shù)。

Example(示例)
template<typename T>
void f()
{
   T x1(1);    // T initialized with 1
   T x0();     // bad: function declaration (often a mistake)

   T y1 {1};   // T initialized with 1
   T y0 {};    // default initialized T
   // ...
}

Enforcement(實(shí)施建議)

  • Flag uses of = to initialize arithmetic types where narrowing occurs.

  • 提示使用=進(jìn)行算數(shù)類型的初始化而且發(fā)生窄化轉(zhuǎn)換的情況。

  • Flag uses of () initialization syntax that are actually declarations. (Many compilers should warn on this already.)

  • 提示使用()初始化語法但實(shí)際上是聲明的情況(很多編譯器應(yīng)該已經(jīng)對(duì)這種情況報(bào)警)

“C++怎么使用{}初始化器語法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI