泛型编程与元编程 [英] Generic programming vs. Metaprogramming

查看:128
本文介绍了泛型编程与元编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到底有什么区别?看来这些术语可以互换使用,但是在阅读Objective-c的维基百科条目时,我发现:

What exactly is the difference? It seems like the terms can be used somewhat interchangeably, but reading the wikipedia entry for Objective-c, I came across:

除了C的程序风格 编程,C ++直接支持 某些形式的面向对象 编程,通用编程和 元编程.

In addition to C’s style of procedural programming, C++ directly supports certain forms of object-oriented programming, generic programming, and metaprogramming.

有关C ++的信息.显然,它们是不同的吗?

in reference to C++. So apparently they're different?

推荐答案

  • 编程:编写用于创建,转换,过滤,聚合和以其他方式处理数据的程序.
  • 元编程:编写用于创建,转换,过滤,聚合和以其他方式操纵程序的程序.
  • 通用编程:编写一个程序来创建,转换,过滤,聚合和以其他方式处理数据,但仅对数据结构进行最小假设,从而在各种应用程序中实现最大程度的重用数据类型.
    • Programming: Writing a program that creates, transforms, filters, aggregates and otherwise manipulates data.
    • Metaprogramming: Writing a program that creates, transforms, filters, aggregates and otherwise manipulates programs.
    • Generic Programming: Writing a program that creates, transforms, filters, aggregates and otherwise manipulates data, but makes only the minimum assumptions about the structure of the data, thus maximizing reuse across a wide range of datatypes.
    • 正如在其他几个答案中已经提到的那样,由于通用编程和(静态/编译时)元编程都是通过模板完成的,因此在C ++中,这种区别可能会造成混淆.为了进一步困扰您,C ++中的泛型编程实际上使用元编程来提高效率,即,模板专业化从通用程序中生成专用(快速)程序.

      As was already mentioned in several other answers, the distinction can be confusing in C++, since both Generic Programming and (static/compile time) Metaprogramming are done with Templates. To confuse you even further, Generic Programming in C++ actually uses Metaprogramming to be efficient, i.e. Template Specialization generates specialized (fast) programs from generic ones.

      还要注意,正如每个Lisp程序员所知道的那样,代码和数据是同一回事,因此实际上并没有元编程"之类的东西,而仅仅是编程.同样,在C ++中很难看到这一点,因为实际上您使用两种完全不同的编程语言进行编程(C ++,C系列中的一种命令性,过程性,面向对象的语言)和元编程(模板,一种纯功能性的偶然性"). 该语言介于纯lambda演算和Haskell之间,语法令人讨厌,因为它实际上从未打算成为编程语言.)

      Also note that, as every Lisp programmer knows, code and data are the same thing, so there really is no such thing as "metaprogramming", it's all just programming. Again, this is a bit hard to see in C++, since you actually use two completely different programming languages for programming (C++, an imperative, procedural, object-oriented language in the C family) and metaprogramming (Templates, a purely functional "accidental" language somewhere in between pure lambda calculus and Haskell, with butt-ugly syntax, since it was never actually intended to be a programming language.)

      许多其他语言在编程和元编程中都使用相同的语言(例如Lisp,Template Haskell,Converge,Smalltalk,Newspeak,Ruby,Ioke,Seph).

      Many other languages use the same language for both programming and metaprogramming (e.g. Lisp, Template Haskell, Converge, Smalltalk, Newspeak, Ruby, Ioke, Seph).

      这篇关于泛型编程与元编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆