QT函数带有外部链接但没有在头文件中声明(QT noreturn属性添加)

Qt noreturn 属性添加

*.cpp:21:1: warning: function ‘f’ could be declared with attribute ‘noreturn’

解决Qt去警告: could be declared with attribute 'noreturn’的方法在函数声明的地方前加
[[noreturn]]
例如:

1
2
3
4
//h文件
[[noreturn]] void test();
//cpp文件
void test(){};

  

原理:

The items between [[ ]] in a function (variable…) declaration are attributes that provide the compiler with extra information about the item they are attached to. The compiler can use these in a range of ways. The “noreturn” attribute has been around since C++ 11.

[[ noreturn ]] attached to a function declaration informs the compiler that control will not return from the function (as in the case of an unconditional throw()). They go in the first declaration of the function. If f() is declared in a header file then this is the place to put it. If the function exists only in a single CPP file, i.e. it is private, with no other declaration then it goes there.

The second code snippet you posted is an example that can lead to undefined behaviour, i.e. returning from a function you explicitly declared would not return control.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// >>> function.h
#ifndef FUNCTION_H
#define FUNCTION_H
  
[[ noreturn ]] void f();
void g();
  
#endif // FUNCTION_H
  
// >>> function.cpp
#include <iostream>
using namespace std;
  
#include "function.h"
  
void f() {
    cout << "No return" << endl;
    throw 1;
}
  
void g() {
    cout << "No return" << endl;;
    throw 1;
}
  
// >>> main.cpp
#include <iostream>
using namespace std;
  
#include "function.h"
  
[[ noreturn ]] void h() {
    cout << "No return" << endl;
    throw 3;
}
  
int main(int argc, char **argv) {
    cout << "Before f()" << endl;
    f();
    // this should be unreachable code
    cout << "Before g()" << endl;
    g();
    cout << "Before h()" << endl;
    h();
}

  Functions f() and g() have identical function code but, because the compiler knows that f() will not return, it may generate different code for these function or calls to them. In my experiments with GNU C++ there is no code generated for the calls to g() and h() because they cannot be reached after a call to f().

 

 

 

 

 

 

  • The items between [[ ]] in a function (variable…) declaration are attributes that provide the compiler with extra information about the item they are attached to.
函数(变量…)声明中[[]]之间的项是属性,为编译器提供了关于它们附加的项的额外信息。
  • The compiler can use these in a range of ways.
编译器可以以多种方式使用这些文件。
  • The “noreturn” attribute has been around since C++ 11.
“noreturn”属性从c++ 11开始就存在了。
  • [[ noreturn ]] attached to a function declaration informs the compiler that control will not return from the function (as in the case of an unconditional throw()).
附加到函数声明的[[noreturn]]通知编译器控制将不会从函数返回(就像无条件抛出()的情况一样)。
  • They go in the first declaration of the function.
它们出现在函数的第一个声明中。
  • If f() is declared in a header file then this is the place to put it.
如果f()是在头文件中声明的,那么这里就是放置它的地方。
  • If the function exists only in a single CPP file, i.e. it is private, with no other declaration then it goes there.
如果函数只存在于单个CPP文件中,即它是私有的,没有其他声明,那么它就会存在于其中。
  • The second code snippet you posted is an example that can lead to undefined behaviour, i.e. returning from a function you explicitly declared would not return control.

你发布的第二个代码片段是一个可能导致未定义行为的例子,例如,从你显式声明的函数返回将不会返回控件。

  

// >>>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function.h
#ifndef FUNCTION_H
#define FUNCTION_H
[[ noreturn ]]voidf();
void g();
#endif
 // FUNCTION_H//
>>>
function.cpp
#include
<iostream>
usingnamespace std;
#include "function.h"
void f()
{
cout <<"No return"<< endl;throw1;}
void g()
{ cout <<"No return"<< endl;;throw1;}// >>> main.cpp#include <iostream>usingnamespace std;#include "function.h"[[ noreturn ]]voidh(){ cout <<"No return"<< endl;throw3;}intmain(int argc,char**argv){ cout <<"Before f()"<< endl;f();// this should be unreachable code cout <<"Before g()"<< endl;g(); cout <<"Before h()"<< endl;h();}

 

  

 

posted @   WinkJie  阅读(755)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix

喜欢请打赏

扫描二维码打赏

支付宝打赏

点击右上角即可分享
微信分享提示