Похожие чаты

Hey there. Does this piece of code cause any UB when

accessing M[0] in the if condition ? (because there's no such a key priorly)

std::map<int, int> M;

if(M[0])
{
++M[0];
}
else
{
M[0] = 0;
}

8 ответов

14 просмотров

You didn't do any initialization here, right? So isn't M just filled with junk? I don't really know. I'm not a C++ guy

Chiyando
You didn't do any initialization here, right? So i...

You're right I think, cause uninitialized objects can cause UB

And, if there is a method for testing if a key exists you probably want to use it

Sahand-🏔️ Автор вопроса
Ujjawal Gupta
You're right I think, cause uninitialized objects ...

I thought it inserts the requested key without causing any ub.

Sahand-🏔️ Автор вопроса
Chiyando
And, if there is a method for testing if a key exi...

Yeah. I can initially insert pairs of "key : 0" into my map, but I'd like to know if there's a better way too.

Sahand-🏔️ Автор вопроса

So is this defined too ?

This does not cause UB. The use of operator[] with a non existent key inserts that key and a value constructed value of value_type

Sahand-🏔️ Автор вопроса

Похожие вопросы

Обсуждают сегодня

А кто-то пробовал, уезжая из Эстонии получить э-рез и продолжить вести предпринимательскую деятельность внутри Эстонии, используя свой OÜ?
Lalalashechki Lalala
62
At which price point will BCH, an ASIC mined coin, be succeptible to reorg attacks because of low hashrate? $10? $1? $0.1? $0.01?
Kishniev
18
Is Kadena onto something special? No one really knows. Good luck!
Bright Uncle Stephen EqualBet
40
зачем же переименовывать ? чтобы кол-во участников возросло или вдруг IBM от этого снова на свифте начнет кодить ? Я не понимаю что страшного в том что свифт гавно, если это т...
Oleh Nerzh
10
Гайз, кто-нибудь пробовал запустить probe-rs под камень, которого нет в probe-rs? Мб есть какой-нибудь пример у кого... Через target-gen попробовал сгенерировать chip-descript...
Максим Смирнов
2
я не магистр хаскеля, но разве не может лейзи тип конвертнуться в не-лейзи запросив вычисление содержимого прям при инициализации?
deadgnom32 λ madao
100
здравствуйте. совершаю вот такую вещь: strcpy(line, (char)current_number); где current number — неподписанный шорт, line — массив чаров. ругань следующая: main.c:29:30: error...
Roberto's Ширгозиев
13
Hello VIC team, I had previously used the “wallet.tomochain.com” with a ledger connection. Unfortunately, the site is no longer accessible and the ledger account is not visibl...
carpe noctem
7
@MrMiscipitlick А можешь макрос написать, который будет вычислять смещение относительно переданных меток? Просто .label1-.label2, и вернуть значение.
КТ315
35
А еще в перле можно уже @arr1 + @arr2?
Sergei Zhmylove
53
Карта сайта