C/C++里的无名
无名,空,匿名,零,都是很有趣的东西。我刚才在看书,感觉可以整理出很多东西来,
大家想到什么就说什么,最好还解释一下其存在的价值。
期望到时候能整理出一个比较完整的关于C/C++语言里 一些无名现象的全集来 我来抛个砖头:
匿名枚举: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 无名对象:#include<iostream>#include<complex>int main(){std::cout<<std::complex<double>(2,3);} do{
}while(0) 也来一个,比如结构体的大小。#include <stdio.h>typedef struct{ charc; int d; short t;}STR;int main(){ STR a; printf("%d\n",sizeof(a)); return 0;} 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;} 通常更加好的定义方法是:typedef struct{ charc; short t; int d;}STR; struct aaa{
int cnt;
int array;
}; struct bbb{
int a;
char b;
int c;
};
// basic idea of offsetof
((struct bbb*)0)->c // unnamed namespace
namespace {
void fun();
}
页:
[1]