为什么 `noexcept` 运算符需要接受表达式而不是函数签名或声明?
考虑以下虚构的示例:
考虑以下虚构的示例:
#include <string>
void strProcessor(const std::string& str) noexcept(true) { };
struct Type{
void method1() noexcept(strProcessor("")) { //Error: Call to nonconstexpr function
strProcessor("");
}
};
它无法编译,因为method1
在其noexcept中具有非constexpr表达式,但是我为什么需要首先在那里放置一个表达式?
我想要做的就是告诉编译器 method1 是noexcept,当且仅当使用成功构造的字符串调用
strProcessor
时也是noexcept(它确实是)。
那么为什么不是
noexcept(void strProcessor(const std :: string&))
呢?
另一个类似的虚拟例子:
struct Type{
Type(bool shouldThrow=false) noexcept(false) { if(shouldThrow) throw "error"; };
void method1() noexcept(true) {};
void method2() noexcept(noexcept(Type().method1())) { method1(); };
}
我想说的是,method2
只有在成功构造 Type 的实例时调用 method1
是无异常的时才是 noexcept (在这种情况下确实是这样),但在定义 method2
时,Type
甚至还没有完全被定义。
如果我对此特性的理解有误,请解释一下。
declval
和相关的内容来避免任何副作用等。尽管我对于noexcept
和constexpr
都不是很熟悉,但我并不真正理解这个错误。 - Lightness Races in Orbit