C++

共14篇文章。

C++中的main定义

Development

新的C++标准中不允许给main指定linkage-specification了。

当然,考虑到原本main()也是C运行环境在开始运行程序的时候调用的,而C运行环境自然也预期C linkage,即不按照C++的习惯对符号根据参数增加名字前缀,因此大部分编译器在遇到C++程序定义全局main()的时候也会按照习惯采取C linkage方式去翻译。这一规则首先被GCC采纳,随后LLVM也跟进了。

阅读全文…:C++中的main定义(本文约455字,阅读大致需要1分钟)

Progress on LLVM clang

Development

As some of you already know, FreeBSD, along with some other Open Source projects, is moving away from GCC due to GPLv3. From a technology perspective, this also makes sense since the (likely) candidate, BSD-style licensed, LLVM based clang, a C compiler, is making reasonably good progress.

So, what should we expect from LLVM or clang?

Here is a good comparison between GCC and clang. Despite GCC is popular, considerably more mature,and supports more languages (especially C++, which clang’s support is incomplete at this moment), clang provides cleaner AST (Abstract Syntax Tree), much more modularized (if you take a look at GCC’s history,Richard Stallman intentionally made GCC’s code not reusable in other software), provided much more information for optimizer, is much faster and use much less memory, etc.

For FreeBSD, currently, clang can compile its kernel with only very few tweaks.

阅读全文…:Progress on LLVM clang(本文约307字,阅读大致需要2分钟)

忍……

Life

前几天clive大长辈发了杨维中的《新同事》给我,Jeff Huang的歌词摘录如左:

喔耶,爆炸吧~爆炸吧!

愛用 .lib嘛… 爆炸吧!
愛亂用global variable嘛… 爆炸吧!
愛亂call function嘛… 爆炸吧!
愛copy & paste嘛… 爆炸吧!

喔耶,我聽到windiff了,爆炸吧!爆炸吧~~~

(在VC6裡,做 .lib時,是不會檢查dependency的。也就是說,如果你呼叫了另外一個 .lib的function,只有compiler會檢查該function有否宣告過,linker不會檢查該symbol是否存在。因此,如果include path有設,header也有 #include,就不會顯示任何錯誤。必須要等到用該 .lib的人,在做 .exe或 .dll時,才會產生missing symbol的錯誤。

另外,.lib相當於是把所有function都赤裸裸地開放出來,這樣就沒有所謂的「模組介面」,因此很容易就會亂呼叫function,而沒有注意到,呼叫了不應有相依關係的component裡的函式。又由於前述不會有missing symbol錯誤的關係,而無法察覺這樣的錯誤,因而導致最終程式的dependency關係會一團亂。

亂call function,除了上面「呼叫了不應有相依關係的component裡的函式」的狀況之外,還包含了「喜歡自行在 .c/.cpp檔前面,重複宣告函式,而不是用 #include將對應的header引入」。這會造成當所依存的component更新,prototype改變時,會無法於compile-time察覺,若僅prototype改變,function name不變的話,甚至連link-time也不會產生error,假使test-time還是無法測到這個問題,那就等著在客戶那邊爆炸。)

阅读全文…:忍……(本文约798字,阅读大致需要2分钟)

4个程序员的一天

Shared Chaos

📓 出处说明

当年转自博客园博主linkcd于2005年7月19日发布的原文《4个程序员的一天》。

【声明:本文没有贬低某个编程语言的意思】

你,一个DotNet程序员,刚刚加入一个新项目组。除了你之外,其他的成员包括:Ceer,一直从事C项目的程序员,他刚刚转入C#不到一个月;Jally,整天抱着本Design Pattern(没错,就是GoF的那本)在啃的前Java程序员;以及Semon,你对他完全不了解,只是听PM介绍说他是搞Scheme的(传说中的第二古老的语言LISP的方言之一)。不过你也没在意,毕竟计算机这玩意,老东西是不吃香的。

周一,刚打开电脑,老板就跑到你们组的办公座面前:「好吧,伙计们,现在有个function需要你们来搞定。具体是这样的:用户输入2个数,并输入一个操作符。你根据输入的情况来得出相应的运算结果。」

Example:Foo(+, 1, 2) = 3; Foo(*, 3, 6) = 18; Foo(/, 2, 4) = 0.5

Ceer最先作出反应:简单嘛,判断一下输入的操作符就好了。说着,他很快在白板上写出如下代码:

阅读全文…:4个程序员的一天(本文约2636字,阅读大致需要6分钟)

How to represent some type inherted from other template, in a C++ template class?

Development

It’s not quite easy to represent a type that is found in base “template class”. Consider the following code:


template <class Key,class T>     class CMyClass: public map<Key,T>{public:    void lookup(Key x)    {        map<Key,T>::iterator iter;         iter = find(x);    }};


阅读全文…:How to represent some type inherted from other template, in a C++ template class?(本文约63字,阅读大致需要1分钟)
搜索文章…(按ESC退出)