gpt4 book ai didi

c++ - 如何向编译器表明函数总是抛出异常?

转载 作者:可可西里 更新时间:2023-11-01 16:25:56 32 4
gpt4 key购买 nike

<分区>

当调用总是从返回值的函数中抛出异常的函数时,编译器通常会警告并非所有控制路径都返回值。合法地如此。

void AlwaysThrows() { throw "something"; }

bool foo()
{
if (cond)
AlwaysThrows();
else
return true; // Warning C4715 here
}

有没有办法告诉编译器 AlwaysThrows 按照它说的去做?

我知道我可以在函数调用后添加另一个 throw:

{ AlwaysThrows(); throw "dummy"; }

而且我知道我可以明确地禁用警告。但我想知道是否有更优雅的解决方案。

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