久久久久久久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成人午夜性a一级毛片 | 亚洲第一黄色网 | av国产精品| 99re视频 | 欧美日韩成人 | 一区二区在线 | 亚洲第1页| 亚洲精品视频在线 | 精品国产欧美一区二区 | 99久久精品国产一区二区三区 | 国产免费视频 | 中文字幕视频在线观看 | 国产黄色精品在线观看 | 精品国产一区二区国模嫣然 | 国产一区二区精品在线观看 | 欧美寡妇偷汉性猛交 | 成人在线不卡 | 亚洲国产成人精品久久久国产成人一区 | 亚洲综合在 | 欧洲成人午夜免费大片 | 天天看天天干 | a在线视频 | 亚洲精品免费观看 | 国产黄色一级片 | 91亚洲国产成人久久精品网站 | 2019精品手机国产品在线 | 在线观看免费av网站 | 二区三区在线观看 | 成人精品福利 | 国产高潮好爽受不了了夜色 | 中文字幕综合 | 成人小视频在线 |