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

使用 std::map<K,V>其中 V 沒有可用的默認

Using std::maplt;K,Vgt; where V has no usable default constructor(使用 std::maplt;K,Vgt;其中 V 沒有可用的默認構造函數)
本文介紹了使用 std::map<K,V>其中 V 沒有可用的默認構造函數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個作為 std::map 實現的符號表.對于值,無法通過默認構造函數合法地構造值類型的實例.但是,如果我不提供默認構造函數,則會出現編譯器錯誤,如果我使構造函數斷言,我的程序可以正常編譯但在 map<K,V>::operator [] 如果我嘗試使用它來添加新成員.

I have a symbol table implemented as a std::map. For the value, there is no way to legitimately construct an instance of the value type via a default constructor. However if I don't provide a default constructor, I get a compiler error and if I make the constructor assert, my program compile just fine but crashes inside of map<K,V>::operator [] if I try to use it to add a new member.

有沒有辦法讓 C++ 在編譯時禁止 map[k] 作為左值(同時允許它作為 r 值)?

Is there a way I can get C++ to disallow map[k] as an l-value at compile time (while allowing it as an r-value)?

順便說一句:我知道我可以使用 Map.insert(map<K,V>::value_type(k,v)) 插入到地圖中.

BTW: I know I can insert into the map using Map.insert(map<K,V>::value_type(k,v)).

一些人提出了相當于改變值類型的解決方案,以便地圖可以在不調用默認構造函數的情況下構造一個.這與我想要的結果完全相反,因為它將錯誤隱藏到以后.如果我愿意,我可以簡單地從構造函數中刪除斷言.我想要是讓錯誤發生得更快;在編譯時.但是,似乎沒有辦法區分 operator[] 的 r-value 和 l-value 使用,所以看起來我想要的東西無法完成,所以我只需要分配一起使用.

several people have proposed solution that amount to altering the type of the value so that the map can construct one without calling the default constructor. This has exactly the opposite result of what I want because it hides the error until later. If I were willing to have that, I could simply remove the assert from the constructor. What I Want is to make the error happen even sooner; at compile time. However, it seems that there is no way to distinguish between r-value and l-value uses of operator[] so it seems what I want can't be done so I'll just have to dispense with using it all together.

推薦答案

你不能讓編譯器區分 operator[] 的兩種用法,因為它們是同一個東西.Operator[] 返回一個引用,因此分配版本只是分配給該引用.

You can't make the compiler differentiate between the two uses of operator[], because they are the same thing. Operator[] returns a reference, so the assignment version is just assigning to that reference.

就我個人而言,除了快速和骯臟的演示代碼之外,我從不使用運算符 [] 來表示地圖.使用 insert() 和 find() 代替.請注意,make_pair() 函數使插入更易于使用:

Personally, I never use operator[] for maps for anything but quick and dirty demo code. Use insert() and find() instead. Note that the make_pair() function makes insert easier to use:

m.insert( make_pair( k, v ) );

<小時>

在 C++11 中,你也可以這樣做


In C++11, you can also do

m.emplace( k, v );
m.emplace( piecewise_construct, make_tuple(k), make_tuple(the_constructor_arg_of_v) );

即使沒有提供復制/移動構造函數.

even if the copy/move constructor is not supplied.

這篇關于使用 std::map&lt;K,V&gt;其中 V 沒有可用的默認構造函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉圖像而不使用 OpenCV 函數)
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設置 SVM 參數)
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 日本一区二区三区在线观看 | 日韩精品一区二区三区中文在线 | 成人在线观看中文字幕 | 国产高清在线精品 | 围产精品久久久久久久 | 欧美三级成人理伦 | 亚洲欧美综合精品另类天天更新 | 国产激情自拍视频 | 一道本不卡视频 | 精品久 | 精品日韩一区二区 | 成人精品鲁一区一区二区 | 91日韩在线 | 国产综合在线视频 | 91精品国产一区二区三区 | 特黄视频 | 国产视频中文字幕 | av喷水| 精品毛片| 久久精品国产一区 | 亚洲午夜精品 | 日本久久黄色 | 国产成人av一区二区三区 | 国产成人艳妇aa视频在线 | 午夜精品久久久久久久99黑人 | 亚洲成人在线视频播放 | 人成在线| 91精品久久久久久久久中文字幕 | 国产精品久久久爽爽爽麻豆色哟哟 | 99在线免费视频 | 欧美国产日韩在线观看成人 | 91视在线国内在线播放酒店 | 欧美一区二区视频 | 精品国产乱码久久久久久久久 | 91视频在线观看免费 | 在线观看黄免费 | 国产亚洲一区二区三区 | 青青久草| 久久久久国产一区二区三区四区 | 久久激情av| 成人免费视屏 |