本文介绍了关于noexceptness的知识应该在传递函数指针时转发吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了以下代码来测试 noexcept 跨函数调用的传播,似乎它不工作,我会想。在GCC 4.7.2中,只有直接或作为模板专用化参数传递时,才能有效地测试函数 noexcept 但是当作为参数传递给模板函数或作为指向正常函数的函数指针时,即使该函数将其形式参数声明为 noexcept 。这里是代码:

  #include< iostream> 

#define test(f)\
std :: cout<< __func__<< :#fis\
<< (noexcept(f())?:not)\
< noexcept\\\
;

template< void(* f)()>
static inline void test0(){
test(f);
}

template< typename F>
static inline void test1(F f){
test(f);
}

static inline void test2(void(* f)()){
test(f);
}

static inline void test3(void(* f)()noexcept){
test(f);
}

void f1(){}
void f2()noexcept {}

int main(){
test );
test(f2);
test0< f1>();
test0< f2>();
test1(f1);
test1(f2);
test2(f1);
test2(f2);
test3(f1);
test3(f2);
return 0;
}

这里输出:

 
main:f1不是noexcept
main:f2是noexcept
test0:f不是noexcept
test0:f是noexcept
test1: f不是noexcept
test1:f不是noexcept
test2:f不是noexcept
test2:f不是noexcept
test3:f不是noexcept
test3: f不是noexcept

为什么 noexcept 在其他情况下不传播?在 test1 的情况下,整个函数是实例化适当类型的 F ,编译器肯定知道该时间是否F是 noexcept 函数。当 noexcept ness声明被完全忽略时,为什么可以写 test3

解决方案

标题必须说明具体的情况吗? C ++ 11标准的15.4.13规定异常规范不被认为是函数类型的一部分。


I have written the following code to test noexcept propagation across function calls, and it seems that it doesn't work as I would have thought. In GCC 4.7.2, A function can effectively be tested against being noexcept only directly or when passed as a template specialization argument; but not when passed as an argument to a templated function, or as a function pointer to a normal function -- even when that function declares its formal parameter as being noexcept. Here's the code:

#include <iostream>

#define test(f) \
    std::cout << __func__ << ": " #f " is " \
              << (noexcept(f()) ? "" : "not ") \
              << "noexcept\n";

template <void(*f)()>
static inline void test0() {
    test(f);
}

template <typename F>
static inline void test1(F f) {
    test(f);
}

static inline void test2(void(*f)()) {
    test(f);
}

static inline void test3(void(*f)()noexcept) {
    test(f);
}

void f1() {}
void f2() noexcept {}

int main() {
    test(f1);
    test(f2);
    test0<f1>();
    test0<f2>();
    test1(f1);
    test1(f2);
    test2(f1);
    test2(f2);
    test3(f1);
    test3(f2);
    return 0;
}

And here's output:

main: f1 is not noexcept
main: f2 is noexcept
test0: f is not noexcept
test0: f is noexcept
test1: f is not noexcept
test1: f is not noexcept
test2: f is not noexcept
test2: f is not noexcept
test3: f is not noexcept
test3: f is not noexcept

Why is noexceptness not propagated in other cases? In case of test1, the whole function is "instantiated" with the proper type of F, the compiler surely knows at that time whether F is a noexcept function. Why is it possible to write test3 the way I wrote it, when the noexceptness declaration is completely ignored?

Does the standard have to say something specific about this?

解决方案

Section 15.4.13 of the C++11 standard states that "an exception-specification is not considered part of a function's type".

这篇关于关于noexceptness的知识应该在传递函数指针时转发吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 02:34