iii 发表于 2024-3-4 06:29:25

C/C++里的无名

无名,空,匿名,零,都是很有趣的东西。
我刚才在看书,感觉可以整理出很多东西来,
大家想到什么就说什么,最好还解释一下其存在的价值。

期望到时候能整理出一个比较完整的关于C/C++语言里 一些无名现象的全集来

Rickykek 发表于 2024-3-4 06:29:46

我来抛个砖头:
匿名枚举:enum { value = 1 , value2 };enum {C,L,O,V,E,R,U,S,I,T};摘自:http://bbs.emath.ac.cn/thread-2606-1-1.html

oasuoxi 发表于 2024-3-4 06:30:46

无名对象:#include<iostream>#include<complex>int main(){std::cout<<std::complex<double>(2,3);}

iniogateln 发表于 2024-3-4 06:31:43

do{
}while(0)

111 发表于 2024-3-4 06:32:11

也来一个,比如结构体的大小。#include <stdio.h>typedef struct{                charc;      int   d;      short t;}STR;int main(){      STR a;      printf("%d\n",sizeof(a));      return 0;}

owobapiakuw 发表于 2024-3-4 06:32:38

5# G-Spider
size 为 7,https://bbs.emath.ac.cn/static/image/smiley/1/biggrin.gif :#include <stdio.h>typedef struct{                charc;      int   d;      short t;}__attribute__((__packed__)) STR;int main(){      STR a={.c='2',.t=123};                        printf("size=%d\n%d\t%d\t%hd\n",sizeof a,a.c,a.d,a.t);      return 0;}

azanuvahejih 发表于 2024-3-4 06:32:55

通常更加好的定义方法是:typedef struct{            charc;       short t;       int   d;}STR;

edzugxoggixo 发表于 2024-3-4 06:33:05

struct aaa{
int cnt;
int array;
};

Kristslots 发表于 2024-3-4 06:33:56

struct bbb{
int a;
char b;
int c;
};
// basic idea of offsetof
((struct bbb*)0)->c

hozewovaxemam 发表于 2024-3-4 06:34:47

// unnamed namespace
namespace {
void fun();
}
页: [1]
查看完整版本: C/C++里的无名