栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++高级 —— 属性

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++高级 —— 属性

文章目录
  • 属性
  • 属性Demo
    • deprecated
    • unused
    • constructor && deconsructor
    • always_inline
  • 参考文献

属性

属性(attribute)是C++中用来控制编译器的编译指令。在C++11之前,非标准的实现中已经出现过属性的申请,比如给gnu的__attribute__或者说vc的__declspec。

C++11中正式将其纳入标准,其并没有新增关键字,而是采用两对方括号的形式[[...]],中间的…内容就是属性标签。C++中常用的属性标签如下:

  • deprecated:这个指明其修饰的函数是一个已经被抛弃的接口,最好换成新接口。
  • unused:用于变量、类型、函数等,表示其虽然暂时不用,但是最好保留下来。
  • constructor:指明此函数会在main函数之前执行
  • destructor:知名此函数会在main函数之后执行
  • always_inline:要求编译器强制内联函数,inline关键字的加强版
  • hot:标记为热点函数,要求编译器更积极优化
属性Demo deprecated

我们定义一个函数为deprecated修饰,这会表示这个函数是个旧的接口。

[[gnu::deprecated]]
int old_func()
{
    return 0;
}

int main()
{
    cout << old_func() << endl;
}

编译的时候编译器会报如下的错误:

[ik@localhost test]$ g++ -o test test.cpp
test.cpp: 在函数‘int main()’中:
test.cpp:13:22: 警告:‘int old_func()’ is deprecated [-Wdeprecated-declarations]
     cout << old_func() << endl;
                      ^
test.cpp:6:5: 附注:在此声明
 int old_func()
     ^~~~~~~~

但是运行还是可以正常运行的:

[ik@localhost test]$ ./test
0
unused

这个关键字告诉编译器这个变量我暂时不会用,但是是我故意的,请不要警告我。

[[gnu::unused]] int var;
int main()
{
    return 0;
}
constructor && deconsructor

我们定义两个函数,at_start会在main函数之前执行,at_exit会在main函数之后执行。

[[gnu::constructor]]
int at_start()
{
    printf("excute before main::funcn");
}

[[gnu::destructor]]
int at_exit()
{
    printf("excute after main::funcn");
}
int main()
{
    cout << "main::func excute!" << endl;
    return 0;
}

注:
constructor修饰的函数不能用cout,因为cout需要全局变量初始化!

输出如下:

[ik@localhost test]$ ./test
excute before main::func
main::func excute!
excute after main::fun
always_inline

这个虽然说是可以强制内联,但是编译器可不会管你的。看一下下面的例子:

[[gnu::always_inline]] void func()
{
    printf("i am test_func()");
}

int main()
{
    func();
    return 0;
}
[ik@localhost test]$ g++ -o test test.cpp
test.cpp:3:29: 警告:always_inline function might not be inlinable [-Wattributes]
 [[gnu::always_inline]] void func()
                             ^~~~
参考文献

[1] 罗剑锋.罗剑锋的C++实战笔记.极客时间

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/664118.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号