首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >递归无例外规范

递归无例外规范
EN

Stack Overflow用户
提问于 2014-05-21 02:13:27
回答 1查看 319关注 0票数 7

使用g++ 4.9和clang3.4进行测试,为什么这段代码不编译:

代码语言:javascript
复制
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);
}

但这段代码确实:

代码语言:javascript
复制
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函数,在这种情况下,它只需要通过解密类型指定正确的返回类型。

第一段代码也为1或2个参数进行编译,但是一旦我尝试用3或更多的参数调用它,就会发现没有匹配函数可调用的错误。第一个代码的clang错误是:

代码语言:javascript
复制
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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-21 02:22:05

3.3.2/1名称的声明点在其完整声明符(第8条)之后,在其初始化器(如果有的话)之前.

异常规范在语法上是声明器的一部分。因此,函数名不在其自身的异常规范范围内。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23772928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档