Assignment in C++ occurs despite exception on the right side

前端 未结 3 1323
遥遥无期
遥遥无期 2021-02-03 17:05

I have some (C++14) code that looks like this:

map> junk;
for (int id : GenerateIds()) {
    try {
        set stuff =         


        
3条回答
  •  温柔的废话
    2021-02-03 17:31

    std::map::operator[]

    Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist.

    junk[id] causes the above mentioned insertion and after that has already happened GetStuff() throws. Note that in C++14 the order in which these things happen is implementation defined so with a different compiler your junk[id] = GetStuff(); may not do the insertion if GetStuff() throws.

提交回复
热议问题