如果函数初始化并返回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"}});
}
答案 0 :(得分:6)
大多数情况下,C ++中的大多数集合类型的构造都可能抛出异常,这是由于分配失败而导致的。因此,返回一个函数也可以抛出,因此不应声明为noexcept
。