C语言复合字面量和.tcc文件

一 概述

  复合字面量是C99标准加入的,在vs2017和g++ 7.5.0中,C++不支持此语法。

二 字面量(literal)

  在计算机科学中,字面量(literal)是用于表达源代码中一个固定值的表示法(notation)(字面量是相对变量常量等定义的,无论是常量还是变量,其值在某一时刻总是确定的,只是变量可以反复赋值、刷新等,然而在运行时,编译期,总是有值的) 。
  几乎所有计算机编程语言都具有对基本值的字面量表示,诸如:整数、浮点数以及字符串;而有很多也对布尔类型和字符类型的值也支持字面量表示;还有一些甚至对枚举类型的元素以及像数组、记录和对象等复合类型的值也支持字面量表示法。
  3 就是 3,值(value)也是 3,,x(变量)便无法通过字面本身取其值。
  字面量就是指这个量本身,比如字面量3。也就是指3. 再比如 string类型的字面量”ABC”, 这个”ABC” 通过字来描述。 string x=”ABC” 意思是把字面量”ABC” 赋值给变量 x。const string y=”cbd”. 意思是把字面量”cbd” 赋值给了常量y。字面量,自己描述自己的量。 “ABC” 它描述了自己,你看到了就知道它是”ABC”了。

三 C语言复合字面量(compound literals)

  

四 示例

#include <stdio.h>
 
struct argv
{
    char a[64];
    int b;
};
 
int func(struct argv a)
{
     printf("argv.a=%s argv.b=%d\n",a.a,a.b);
} 
 
int main(int argc,char *argv[])
{
    struct argv a = {
      .a = "chenhongyu",
      .b = 24
    };
 
    func(a);
 
    func((struct argv){"chenhongyu",24});
    
    return 0;
}

  运行结果:
在这里插入图片描述

五 .tcc文件

  It’s not standardised. Some use .tcc (t = template, cc = C++ source),some use .icc (i = included, cc = C++ source), some use .h (but that makes it indistinguishable from “real” headers), some just don’t use an extra file at all. It’s usually a file that contains implementations of templates declared in a header that then #includes the .tcc (or whatever) file. But as there is no standard nor a general consense about this, it could be anything else as well.It’s just an artifact of the need to have template definitions visible in all translation units that use them with most compilers:
example.hpp

#ifndef EXAMPLE_HPP
#define EXAMPLE_HPP

template< typename T > void f();

#include "example.tcc"

#endif

example.tcc

#ifndef EXAMPLE_HPP
# error Don't include this file directly, include example.hpp instead
#endif

template< typename T > void f()
{
// stuff
}

  非标准化的文件。就像经常使用的.tcc(t = template, cc = C++ source),.icc(i = included, cc = C++ source),.h(没法从“真正”的头文件中区分开来),有些根本不用额外的文件。这种文件中包含模版的声明和实现,通常用#include的方式包含。但由于没有标准,也没有一个普遍的意义,它可以是任何其他的东西。它只是需要模板定义在大多数编译器中使用的所有翻译单元中可见的产物。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值