久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么花括號(hào)初始化器的自動(dòng)和模板類(lèi)型推導(dǎo)不

Why do auto and template type deduction differ for braced initializers?(為什么花括號(hào)初始化器的自動(dòng)和模板類(lèi)型推導(dǎo)不同?)
本文介紹了為什么花括號(hào)初始化器的自動(dòng)和模板類(lèi)型推導(dǎo)不同?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

我知道,給定一個(gè)花括號(hào)初始化器,auto 將推導(dǎo)出 std::initializer_list 的類(lèi)型,而模板類(lèi)型推導(dǎo)將失敗:

auto var = { 1, 2, 3 };//類(lèi)型推導(dǎo)為 std::initializer_list模板void f(T 參數(shù));f({ 1, 2, 3 });//不編譯;類(lèi)型推導(dǎo)失敗

我什至知道這是在 C++11 標(biāo)準(zhǔn)中指定的地方:14.8.2.5/5 bullet 5:

<塊引用>

[如果程序有,它是一個(gè)非推導(dǎo)的上下文]一個(gè)函數(shù)參數(shù),它的關(guān)聯(lián)參數(shù)是一個(gè)初始化列表(8.5.4)但是參數(shù)沒(méi)有 std::initializer_list 或?qū)赡苡?cv 限定的 std::initializer_list 的引用類(lèi)型.[ 示例:

模板 void g(T);

g({1,2,3});//錯(cuò)誤:沒(méi)有為 T 推導(dǎo)出參數(shù)

結(jié)束示例 ]

我不知道或不明白的是為什么存在類(lèi)型推導(dǎo)行為的這種差異.C++14 CD 中的規(guī)范與 C++11 中的規(guī)范相同,因此大概標(biāo)準(zhǔn)化委員會(huì)不會(huì)將 C++11 行為視為缺陷.

有誰(shuí)知道為什么 auto 推導(dǎo)出一個(gè)花括號(hào)初始化器的類(lèi)型,但不允許模板?雖然對(duì)這可能是原因"這種形式的推測(cè)性解釋很有趣,但我對(duì)那些知道標(biāo)準(zhǔn)為何如此編寫(xiě)的人的解釋特別感興趣.

解決方案

模板不做任何演繹的重要原因有兩個(gè)(我記得和負(fù)責(zé)人討論的兩個(gè))

  • 對(duì)未來(lái)語(yǔ)言擴(kuò)展的擔(dān)憂(您可以發(fā)明多種含義 - 如果我們想為帶括號(hào)的初始化列表函數(shù)參數(shù)引入完美轉(zhuǎn)發(fā)呢?)

  • 大括號(hào)有時(shí)可以有效地初始化一個(gè)依賴的函數(shù)參數(shù)

<塊引用>

template無(wú)效分配(T&d,const T& s);

int main() {向量v;分配(v, { 1, 2, 3 });}

如果 T 將在右側(cè)推導(dǎo)出到 initializer_list 但在左側(cè)推導(dǎo)出 vector,這會(huì)因?yàn)樽韵嗝艿恼撟C推論而失敗.

autoinitializer_list 的推導(dǎo)是有爭(zhēng)議的.存在 C++-after-14 刪除它的提議(并禁止使用 { }{a, b} 進(jìn)行初始化,并使 {a} 推導(dǎo)出a的類(lèi)型).

I understand that, given a braced initializer, auto will deduce a type of std::initializer_list, while template type deduction will fail:

auto var = { 1, 2, 3 };   // type deduced as std::initializer_list<int>

template<class T> void f(T parameter);

f({ 1, 2, 3 });          // doesn't compile; type deduction fails

I even know where this is specified in the C++11 standard: 14.8.2.5/5 bullet 5:

[It's a non-deduced context if the program has] A function parameter for which the associated argument is an initializer list (8.5.4) but the parameter does not have std::initializer_list or reference to possibly cv-qualified std::initializer_list type. [ Example:

template void g(T);

g({1,2,3}); // error: no argument deduced for T

end example ]

What I don't know or understand is why this difference in type deduction behavior exists. The specification in the C++14 CD is the same as in C++11, so presumably the standardization committee doesn't view the C++11 behavior as a defect.

Does anybody know why auto deduces a type for a braced initializer, but templates are not permitted to? While speculative explanations of the form "this could be the reason" are interesting, I'm especially interested in explanations from people who know why the standard was written the way it was.

解決方案

There are two important reasons for templates not to do any deduction (the two that I remember in a discussion with the guy in charge)

  • Concerns about future language extensions (there are multiple meanings you could invent - what about if we wanted to introduce perfect forwarding for braced init list function arguments?)

  • The braces can sometimes validly initialize a function parameter that is dependent

template<typename T>
void assign(T &d, const T& s);

int main() {
  vector<int> v;
  assign(v, { 1, 2, 3 });
}

If T would be deduced at the right side to initializer_list<int> but at the left side to vector<int>, this would fail to work because of a contradictional argument deduction.

The deduction for auto to initializer_list<T> is controversial. There exist a proposal for C++-after-14 to remove it (and to ban initialization with { } or {a, b}, and to make {a} deduce to the type of a).

這篇關(guān)于為什么花括號(hào)初始化器的自動(dòng)和模板類(lèi)型推導(dǎo)不同?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡(jiǎn)單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問(wèn)向量的內(nèi)容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對(duì)普通變量進(jìn)行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會(huì)導(dǎo)致訪問(wèn)沖突?)
主站蜘蛛池模板: www久久av | 俺去俺来也www色官网cms | 久久国产精品视频 | 日本在线中文 | www国产成人 | 天天曰夜夜操 | 一级毛片视频 | 中文字幕精品一区 | 国产精品色综合 | av一级久久 | 欧美日韩在线播放 | 欧美一区二区三区在线观看视频 | 91欧美激情一区二区三区成人 | 九色在线视频 | 粉嫩粉嫩芽的虎白女18在线视频 | 国产色网| 日本高清中文字幕 | 黄色三级免费网站 | 久热精品视频 | 免费毛片在线 | 国产精品夜夜春夜夜爽久久电影 | 亚洲欧洲在线看 | 91视频在线 | 成人免费福利 | 成人日b视频 | 亚洲视频免费播放 | 久久a久久 | 日韩精品视频在线观看一区二区三区 | 久久蜜桃av| 精品国产一级 | 精品国产乱码一区二区三区a | www.日日夜夜| 91人人在线 | 欧美视频免费 | 日韩精品久久久 | 久久久久久久亚洲精品 | 欧美在线视频网 | 久久国产精品一区 | 在线观看国产 | 欧美亚洲另类丝袜综合网动图 | 夜夜精品浪潮av一区二区三区 |