gpt4 book ai didi

c++ - 在返回 unordered_map 的函数上使用 `noexcept`

转载 作者:行者123 更新时间:2023-11-30 00:43:29 24 4
gpt4 key购买 nike

将函数声明为 noexcept 是否安全?如果它初始化并返回 std::unordered_map<int, string> .例如:

std::unordered_map<int, std::string> get_raw_num_map() noexcept
{
return std::unordered_map<int, std::string>(
{{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}, {6, "six"}, {7, "seven"}, {8, "eight"},
{9, "nine"}, {10, "ten"}, {11, "eleven"}, {12, "twelve"}, {13, "thirteen"}, {14, "fourteen"}, {15, "fifteen"},
{16, "sixteen"}, {17, "seventeen"}, {18, "eighteen"}, {19, "nineteen"}, {20, "twenty"}, {30, "thirty"},
{40, "forty"}, {50, "fifty"}, {60, "sixty"}, {70, "seventy"}, {80, "eighty"}, {90, "ninety"},
{100, "hundred"}, {1000, "thousand"}});
}

最佳答案

C++ 中大多数集合类型的构造都可能抛出异常,如果没有其他原因,分配失败。所以返回一个的函数也可以抛出,不应该被声明为noexcept

关于c++ - 在返回 unordered_map 的函数上使用 `noexcept`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54029789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com