存档

文章标签 ‘c++’

C++ Template Corner Cases

2009年7月10日 davidzhao 没有评论

各位读者,很抱歉这篇文章是英文的,我当初做笔记的时候,写成英文了,这样才可以在同事之间交流。而现在确实没时间翻译过来了,还望大家理解,谢谢!

Following are some corner cases of C++ template features. A lot of the text is simply extracted from “C++ Templates: The Complete Guide”, with some of my personal understanding. These features are trivial and easily neglected, but you should have some impression to them in case you run into troubles caused by the neglect.

I made my notes in English, and I don’t bother to translate them into Chinese, forgive my laziness. :)

0. use “typename” to extract type defined within a type parameter, like this: typename T::iterator itr; otherwise the ‘itr’ is treated as a data member of T.

1. zero initialization
When using a var x of type T, we MUST initialize it like this: T x = T(); so that when T is a primitive type, x can also be initialized with 0, or NULL (when T is a pointer type). Of course we must make sure
when T is a class type, it has a default constructor. Simply using T x; can’t initialize x when T is a primitive type. 阅读全文…

分类: David Zhao, 程序设计 标签:

编写可移植的C++ 模板代码

2009年1月7日 davidzhao 没有评论

这篇文章最早发布在我的百度博客上的( http://hi.baidu.com/dazhao%5Fdbblog ),现在把它贴过来。

MSVC8 对c++模板的支持,基本上遵循了c++标准的规定,不过在某些细节之处仍然超越了c++标准,多做了一些工作,从而一定程度上减轻了程序员编程的负担。但是这种简化,是一把双刃剑—当你使用MSVC8编程的时候你会感觉轻松而简便,你不知不觉中就陷入了MS的陷阱—当你需要把代码移植到使用gcc的*nix上时候,你将遇到很多头疼的问题,甚至不得不放弃,从而困在微软的小世界里面。为了防患于未燃,我们应该在编码之前,就知道如何编写可移植的c++模板代码,不要使用MSVC8的扩展。我把我工作中学到的经验列出来,供大家参考,这样大家就不会犯我曾经犯过的错误。要知道,为了修正这些不可移植的代码,我用去了整整一个星期。 阅读全文…

分类: David Zhao 标签:
Դ