cmake+gcc解决extended initializer lists only available with -std=c++11 or -std=gnu++11
2015-10-11 15:22
453 查看
有时,我们需要一个结构体变量做为参数传递给函数,后面又不需要再用到这个结构体变量,所以如果专门定义一个变量,再将这个变量传递给函数就显得挺罗嗦。
就像下面这段代码,
这时我们可以将数据包装成临时结构体变量(或者叫匿名变量)传递给要调用函数。所以调用方式可以简化成下面这样:
但是重新编译,会出下面警告
J:\workspace\codemgr\cmimpl\testmd5.cpp: In function ‘int main()’:
J:\workspace\codemgr\cmimpl\testmd5.cpp:26:11: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
test(_stA{str,0,1});
它的意思大概就是_stA{str,0,1}这种用列表初始化结构的方式要在c++ 11标准中才支持。
initializer lists是c++11的特性 (参见 std::initializer_list(点击打开链接))
so,对于cmake生成的项目,我们就需要在CMakeLists.txt中添加如下编译选项
add_definitions(“-std=c++11”)
或者
add_definitions(“-std=gnu++11)
然后重新生成makefile,编译警告就消失了。
就像下面这段代码,
tmpStruct只是起到一个数据包装的作用,调用
test()之后并不再用到,所以这个变量就显示有些多余 。
#include <iostream> struct _stA{ char *a; int b; int c; }stA; void test(_stA n){ cout<<n.a<<endl; } int main() { char str[]="hello!"; _stA tmpStruct{str,0,1};//定义一个变量装载str等数据; //或者写成_stA tmpStruct={str,0,1}也是可以的 test(tmpStruct);//将tmpStruct做为变量传递给test }
这时我们可以将数据包装成临时结构体变量(或者叫匿名变量)传递给要调用函数。所以调用方式可以简化成下面这样:
int main() { char str[]="hello!"; test(_stA{str,0,1});//编译器会为_stA{str,0,1}结构体生成一个临时变量 }
但是重新编译,会出下面警告
J:\workspace\codemgr\cmimpl\testmd5.cpp: In function ‘int main()’:
J:\workspace\codemgr\cmimpl\testmd5.cpp:26:11: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
test(_stA{str,0,1});
它的意思大概就是_stA{str,0,1}这种用列表初始化结构的方式要在c++ 11标准中才支持。
initializer lists是c++11的特性 (参见 std::initializer_list(点击打开链接))
so,对于cmake生成的项目,我们就需要在CMakeLists.txt中添加如下编译选项
add_definitions(“-std=c++11”)
或者
add_definitions(“-std=gnu++11)
然后重新生成makefile,编译警告就消失了。
相关文章推荐
- 在Windows平台下使用安装GCC(图)
- linux使用gcc编译c语言共享库步骤
- C/C++编译器GCC下的常用编译命令总结
- GCC 编译c程序的方法及过程解析
- Glusterfs的编译选项 #pragma GCC poison system popen
- cmake clion 构建cocos2dx 应用程序并编译ios mac android
- 【笔记】CentOS上源码安装GCC 4.8.2
- 【笔记】CentOS
- RHEL 4.7 (64bit) 环境安装 GCC 4.6 测试记录 (更新至gcc-4.6.1)
- OpenCV配置,从来没有这么简单!
- 在 Qt4 中使用 C++11
- GCC 5 即将发布
- [转载]Visual Studio, GCC, Clang, Intel 对 C++11 特性的支持一览表
- C++爱好者博客
- gcc工具的运用
- gdb的运用
- GCC内联汇编(1)Get started
- GCC内嵌汇编
- g++编译 参数 .