底下是改寫自 stackoverflow 的各種有趣寫法,
原始創意乃源出於不同作者:
Zeroing structs without memset:
FStruct s = {0}; 或 FStruct s = {};
void search (char* query) { http://www.google.com/search?q=query return; }然後... Peter Mortensen 這樣寫到:
Fred* f = new(ram) Fred(); http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.10 f->~Fred();同樣是 Peter Mortensen,身份變換 :-)
class A {}; struct B { A a; operator A&() { return a; } }; void func(A a) { } int main() { A a, c; B b; a = c; func (b); //yeah baby a = b; //gotta love this }Daniel Martin,Perl 運算式解析
sub e{($_)=@_;$n='( *-?[.\d]++ *)'; s:\($n\)|$n(.)$n:($1,0,$2*$4,$2+$4,0,$2-$4)[7&ord$3]//$2/$4:e?e($_):$_}gnibbler,Golfscript 文字模式 Sierpinski’s Triangle:
' /\ /__\ '4/{).+: ;.{ \ ++}%\{.+}%+~ ]}@~(*n*惡作劇用:
<td align="right" style="text-align: left">也是惡作劇用,fork bomb:
:(){ :|:& };:放在 source code 最上方.. 然後執行它:
//&>/dev/null;x="${0%.*}";[ ! "$x" -ot "$0" ]||(rm -f "$x";cc -o "$x" "$0")&&"$x" $*;exit
In Quake 2 src:double normals[][] = { #include "normals.txt" };Multi-character constants:
enum state { stopped = 'STOP', running = 'RUN!', waiting = 'WAIT', };三元運算:
(a? b: c) = (b<c ? b: c);Patrick post,同樣是三元運算 (q = b ? x : y)
bool b; int x, y; int q = -!!b&x|-!b&y;算 PI:
double fldpi () { double pi; asm ("fldpi" : "=t" (pi)); return pi; }Static linked lists:
struct llist { int a; struct llist* next;}; #define cons(x,y) (struct llist[]) {{x,y}} struct llist *list = cons(1, cons(2, cons(3, cons(4, NULL))));整數求和:
#define sum(...) \ _sum(sizeof((int []){ __VA_ARGS__ })\ / sizeof(int), (int []){ __VA_ARGS__ }) int _sum (size_t count, int values[]) { int s = 0; while (count--) s += values[count]; return s; } int main () {printf("%i", sum (1,2,3));}上例的變形:
void f (int len, int *n, char c) {...} #define SIZEOF(a) sizeof(a)/sizeof*(a) #define NOPAREN(...) __VA_ARGS__ #define F1(arr,c) {int v[]={NOPAREN arr}; f(SIZEOF(v),v,c);} #define F2(c,...) {int v[]=__VA_ARGS__; f(SIZEOF(v),v,c);}再一種應用:
#include <algorithm> #define MAX(x, ...) ({\ typeof(x) ra[] = {(x), __VA_ARGS__}; \ *std::max_element (&ra[0], &ra[sizeof ra/sizeof 0[ra]]); \ }) int main() { int i = 12; std::cout << MAX (i+1,1,3,6,i); }Johannes Schaub 寫的:
template <template From, template To> union union_cast { From from; To to; union_cast (From from): from(from) { } To getTo() const { return to; } };這是真的嗎 :-)
const MyClass& x = MyClass(); // temporary exists as long as x is in scopeRube 累計遞增:
def foo(n) lambda { |i| n += i } end測驗題,不用空白來定義結構。By Bojan Resnik:
typedef/**/struct{const/**/char/**/c;unsigned/**/u;}nospace;vividos 寫的:
int Func() try { // if success return 0; } catch(...) { return -1; }goto 和 dtor 的關係:
struct A { ~A() {cout<<"dtor";} }; int main() { { A a; goto L; } L: cout<<"After L"; }Evil C:
int logical_xor (int a, int b) { return !a != !b; }From Canadian Mind Products:
char *p; switch (n) { case 1: p = "one"; if (0) case 2: p = "two"; if (0) case 3: p = "three"; printf ("%s", p); break; }自動初始化 a 中元素為 0(那麼,本來會做嗎?):
class C { int a[10]; C(): a() {} };template bitfields,by Kaz Dragon:
template <size_t X, size_t Y> struct bitfield { char left : X; char right : Y; };出自 Comptrol:
template <typename T> class Creator { friend void appear() { // a new function ::appear(), but it doesn't … // exist until Creator is instantiated } }; Creator<void> miracle; // ::appear() is created at this point Creator<double> oops; // ERROR: ::appear() is created a second time! template <typename T> class Creator { friend void feed(Creator<T>*){ // every T generates a different … // function ::feed() } }; Creator<void> one; // generates ::feed(Creator<void>*) Creator<double> two; // generates ::feed(Creator<double>*)用於 non-tmpl 的類 boost::identity。
出自 Johannes Schaub:
template<typename T> struct id { typedef T type; }; // void (*f)(); // same id<void()>::type *f; // void (*f(void(*p)()))(int); // same id<void(int)>::type *f(id<void()>::type *p); // int (*p)[2] = new int[10][2]; // same id<int[2]>::type *p = new int[10][2]; // void (C::*p)(int) = 0; // same id<void(int)>::type C::*p = 0;原載於 Sumant Tambe 的 Blog:
template<class T> // (a) a base template void f( T ) { std::cout << "f(T)\n"; } template<> void f<>(int*) { // (b) an explicit specialization std::cout << "f(int *) specilization\n"; } template<class T> // (c) another, overloads (a) void f( T* ) { std::cout << "f(T *)\n"; } template<> void f<>(int*) { // (d) another identical explicit specialization std::cout << "f(int *) another specilization\n"; }用複雜方法做簡單的事? 出自 Marcus Lindblom:
template<int> class foo; template<> struct foo<0> { int* get() { return array; } int* array; }; template<int i> struct foo: public foo<i-1> { int* get() { return array + 1; } };
沒有留言:
張貼留言