int bar; void foo() {} class Foo { static int value; void Bar(int); }; int Foo::value = 0; void Foo::Bar(int) {}
Linux
在Linux底下可以用nm來指令
1
$ nm -gC test.so
g 列出所有External symbols
c 由於C++支援Function overload,要區別同名的函數名稱的話,需要在後方加上一些符號以供區別,而Demangle就是讓這些奇形怪狀的符號變成人可以看懂得版本。 以上的結果如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14
000000000020102c B bar 0000000000201028 B __bss_start w __cxa_finalize@@GLIBC_2.2.5 0000000000201028 D _edata 0000000000201038 B _end 00000000000006e4 T _fini w __gmon_start__ 00000000000005a0 T _init w _ITM_deregisterTMCloneTable w _ITM_registerTMCloneTable w _Jv_RegisterClasses 00000000000006d0 T foo() 00000000000006d6 T Foo::Bar(int) 0000000000201030 B Foo::value
#include <stdio.h> int main() { long long s = 0; long long count; scanf("%lld", &count); for (long long i = 1; i <= count; i++) s += i; printf("%llu\n", s); return 0; }
class Base { } class Derived : Base { } public static void Run(IEnumerable<Base> bases) { } IEnumerable<Derived> derivedBases = new Derived[] { new Derived(), new Derived() }; Run(derivedBases);
類似的程式碼在C++就失敗了…
Covariance Example lang: c#
1 2 3 4 5 6 7 8 9 10 11 12
class Animal { public void Feed() {} } class Frog : Animal { } static void DoSomethingToAFrog(Action<Frog> action, Frog frog) { action(frog); } Action<Animal> feed = animal => { animal.Feed(); }; DoSomethingToAFrog(feed, new Frog());
跟數學裡面Set comprehension的表達方法很像,假設我們想要找出直角三角形中,周長小於50的三元組的數量。 (假設 a <= b <= c 且 a ^ 2 + b ^ 2 = c ^ 2)。
如果是以往的作法,大概會像這樣
1 2 3 4 5 6 7 8 9
for (int a = 1; a <= 10; a++) #define sqr(x) (x * x) typedef tuple<int, int, int> tuple3; std::vector<tuple3> ans; for (int a = 1; a <= 50; a++) for (int b = a; b <= 50; b++) for (int c = b; c <= 50; c++) if ((sqr(a) + sqr(b) == sqr(c)) && ((a + b + c) < 50)) ans.push_back(make_tuple(a, b, c));