We don't have the full scenario to determine exactly whether that function will be able to throw.
Your assumptions are though correct: since you're doing what std::vector::at
will anyway do, it is unlikely to be harmful in the given context. However, generally talking, such function may be subject of invalidating factors and thus potentially able to throw an exception. These could be threads, processes or signals that could interleave the execution to code that might modify std::vector
, which can't handle that safely and neither can std::vector::at
.
If you want to consider those possibilities as well, you'll need to have something like
int get_value_or_default(const std::vector<int>& v, size_t idx) noexcept
{
try { return v.at(idx); }
catch (std::out_of_range const& e) { return -1; }
}