递归noexcept规范
Recursive noexcept specification
在g++ 4.9和clang 3.4下测试,为什么这段代码不能编译:
namespace {
template<typename T>
constexpr auto f(T && t) noexcept {
return true;
}
template<typename T, typename... Ts>
constexpr auto f(T && t, Ts && ... ts) noexcept(noexcept(f(ts...))) {
return f(ts...);
}
} // namespace
int main() {
f(true, 0, 5u);
}
但是这段代码做了:
namespace {
template<typename T>
constexpr auto f(T && t) noexcept {
return true;
}
template<typename T>
constexpr auto f_helper(T && t) noexcept(noexcept(f(t))) {
return f(t);
}
template<typename T, typename... Ts>
constexpr auto f_helper(T && t, Ts && ... ts) noexcept(noexcept(f(ts...))) {
return f(ts...);
}
template<typename T, typename... Ts>
constexpr auto f(T && t, Ts && ... ts) noexcept(noexcept(f_helper(ts...))) {
return f(ts...);
}
} // namespace
int main() {
f(true, 0, 5u);
}
不需要定义f_helper函数,在这种情况下,它只需要具有通过decltype指定的正确返回类型。
第一个代码也可以编译1或2个参数,但是一旦我尝试用3个或更多的参数调用它,我就会得到关于没有匹配函数可调用的错误。第一个代码的clang错误是:
source/main.cpp:9:59: error: call to function 'f' that is neither visible in the template definition nor
found by argument-dependent lookup
constexpr auto f(T && t, Ts && ... ts) noexcept(noexcept(f(ts...))) {
^
source/main.cpp:9:17: note: in instantiation of exception specification for 'f<bool, int, unsigned int>'
requested here
constexpr auto f(T && t, Ts && ... ts) noexcept(noexcept(f(ts...))) {
^
source/main.cpp:16:3: note: in instantiation of function template specialization '<anonymous
namespace>::f<bool, int, unsigned int>' requested here
f(true, 0, 5u);
^
source/main.cpp:9:17: note: 'f' should be declared prior to the call site
constexpr auto f(T && t, Ts && ... ts) noexcept(noexcept(f(ts...))) {
^
1 error generated.
3.3.2/1名称的声明点紧接在它的完整声明符(第8条)之后,在它的初始化式(如果有的话)之前…
exception-specification在语法上是声明符的一部分。因此,函数名不在它自己的异常规范范围内。
相关文章:
- 提升精神:解析布尔表达式并简化为规范范式
- 在noexcept 规范中是否允许使用"this"?
- 使用 noexcept 运算符 depenendet
- 模板规范获取'Ambiguous call to overloaded function'
- 哪个C++规范开始支持 std::vector
- C++部分概念 id:显式模板规范顺序/第一个参数的特殊状态的原因是什么?
- C++中"覆盖功能的异常规范比基本版本更宽松"的奇怪错误
- NOEXCEPT 函数调用运算符的说明符_Not_fn
- 参数包内 noexcept 说明符
- 了解标准::矢量规范
- 运算符+ 的规范实现涉及额外的移动构造函数
- 当 noexcept 函数尝试在 gcc 或 clang 中调用非 noexcept 函数时启用警告
- std::complex<> in C++ 数学特殊函数:技术规范或提案
- 为什么使用 std::vector 的代码不能编译,但使用 std::unique_ptr 如果没有 noexcept
- C++规范是否特别对待标准 t 库?
- 为什么旧的空抛规范被用新的语法"noexcept"重写?
- "CalucateNumbers"缺少异常规范"noexcept"
- 如何编写支持adl的尾随返回类型或noexcept规范
- noexcept(false)析构函数覆盖所有特殊成员函数'异常规范
- 递归noexcept规范