zoukankan      html  css  js  c++  java
  • 复合文字(Compound Literals)

    复合文字(Compound Literals)

    阅读代码时发现了这行

    1
    setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&(int){1},sizeof(int));

    发现之前没有见过这种写法

    1
    &(int){1}

    上网搜索发现Compound Literalsc99添加的新特性,gccc90c++中也支持此种特性,但在c++中的语义有所不同.

    官网上举的例子是:

    假如一个结构体的定义如下:

    1
    2
    3
    4
    struct {
    int a;
    char b[2];
    }structure;

    那么使用复合文字构造一个结构体的方法如下:

    1
    structure = ((struct foo){x + y,'a',0});

    这相当于:

    1
    2
    3
    4
    {
    struct temp = {x + y, 'a', 0};
    structure = temp;
    }

    验证下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16


    struct {
    int a;
    char b[2];
    }structure;

    int main()
    {
    int x = 1;
    int y = 2;
    structure = ((struct foo){x + y,'a',0});
    printf("stu.a:%dn",structure.a);
    printf("stu.b:%sn",structure.b);
    return 0;
    }

    在我的系统上:

    大专栏  复合文字(Compound Literals)">
    1
    2
    3
    4
    5
    6
    [test-dir] uname -r
    3.10.0-693.17.1.el7.x86_64
    [test-dir] gcc -version
    gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)
    [test-dir] cat /etc/redhat-release
    CentOS Linux release 7.4.1708 (Core)

    编译通过:

    1
    gcc test.c -o test

    运行:

    1
    2
    3
    [test-dir] ./test                                                     
    stu.a:3
    stu.b:a

    还可以用来初始化二维数组:

    1
    char **foo = (char *[]) { "x", "y", "z" };

    变量:

    1
    int i = ++(int) { 1 };

    作为GNU的拓展,gcc允许通过这用特性初始化静态变量

    1
    2
    3
    static struct  x = (struct ) {1, 'a', 'b'};
    static int y[] = (int []) {1, 2, 3};
    static int z[] = (int [3]) {1};

    相当于:

    1
    2
    3
    static struct foo x = {1, 'a', 'b'};
    static int y[] = {1, 2, 3};
    static int z[] = {1, 0, 0};

    在C中,复合文字指定具有静态或自动存储时间的未命名对象,而在C++中代表一个临时的对象,生命周期只能到其表达式的末尾。结果就是在C中复合文字中的子对象会被分配地址,在C++中则是未定义的行为,因此g++不会为临时的数组转化为指针。

    例如,如果数组复合文字foo的实例出现在函数内部,在C++中后续任何使用foo的结果都是未定义的,因为数组foo的生命周期在其声明之后就结束了。

    作为优化,在g++中有时会给复合文字数组更长的生命周期,例如在函数之外或者用const修饰,如果foo及其初始化程序具有char * const类型而不是char *类型的元素,亦或者foo是全局变量,这个数组则有静态存储的生命周期,但是,避免在C++代码中使用复合文字是最安全的。

  • 相关阅读:
    浅析区间问题
    关于参加信息竞赛
    路爷爷语录
    CentOS-7安装python虚拟环境
    开发笔记(不定时更新)
    Redis有序集合的权重值选择
    数据缓存(Redis)流程设计--思路
    Flask框架之配置日志信息
    Flask框架之一对多关系模型定义
    Flask框架之多对多关系模型类的定义
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12041284.html
Copyright © 2011-2022 走看看