Meta programming 学习〈一〉Meta programming 是一种新语言,一种新的编程方式

    搞了好几年的C++,自认为谈不上精通也算比较熟悉了吧,但对c++中的template却一直没有深入玩过,对template的印象也仅仅停留在类型参数化,还有模板特化这一点点认识。

  然看到STL,BOOST, GTEST库中错综复杂的template用法,一边叹为观止确又一边晦莫如深。而今终于鼓起勇气,准备闯一闯这遍桃花源。


  入门教材:

  《C++.Template.Metaprogramming》

  《Beyond+C.plus.plus.Standard.Library.An.Introduction.to.Boost.chm》


  如果只是单纯把template看到是C++提供的一个高级语法格式,那么你可能也会如我一般,刚开始就一头扎进boost源码,弄得一个头两个大。直到看了C++.Template.Metaprogramming, 才发现这里面大有学问。人家叫meta programming.

        Template 可以用来做meta programming. meta有元始,超越的的意思。什么是meta programming, 我感觉现在对它的认识还有些浅,先引用wikipedia的解释

  Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves) as their data, or that do part of the work at compile time that would otherwise be done at runtime.

  元编程有两种:

  1. 程序代码做为其操作的数据来做编程,即用程序来生成程序

  2. 编绎时计算(将本来是在运行时做的运算提前到编绎时做)

  如果还是很抽象,可以想下java/C#的 relection即meta programming.

  我们把用template来写代码叫meta language,把正常的语言写代码叫object lanugage,所以meta programming相当于使用一种新语言,以一种新的思维方式来编程。

   

你可能感兴趣的:(meta,programming,template)