元编程 Metaprogramming

简介: 概念 Metaprogramming is writing code that writes code. Metaprogramming is writing code that manipulates language constructs at runtime.

概念

Metaprogramming is writing code that writes code.

Metaprogramming is writing code that manipulates language constructs

at runtime.

可以编写代码的代码

 

Metaprogramming Ruby

This particular brand of metaprogramming implies that you use a program to generate or otherwise manipulate a second, distinct programand then you run the second program. 

After you run the code generator, you can actually read the generated code and (if you want to test your tolerance for pain) even modify it by hand before you finally run it. This is also what happens under the hood with C++ templates: the compiler turns your templates into a regular C++ program before compiling them, and then you run the compiled program.

code that manipulates itself at runtime. Only a few languages can do that effectively, and Ruby is one of them. You can think of this as dynamic metaprogramming to distinguish it from the static metaprogramming of code generators and compilers.

 

元编程是编写在运行时操纵语言构件的代码。
  ActiveRecord的作者是怎样应用这个概念的?他不是为每个类的属性编写访问器方法,而是编写代码为每个继承自ActiveRecord::Base的类在运行时定义方法。这就是“编写代码的代码”时所指的东西。 

一个用C语言写的程序会跨越两个不同的世界:编译时和运行时。在编译时,可以拥有像变量和函数这样的语言构件;而在运行时,只有一大堆机器码。由于绝大多数编译时的信息在运行时都丢失了,所以C语言不支持元编程或内省。在C++语言中,一些语言构件可以在编译后生存下来,这也是为什么你可以向C++对象询问它的类的原因。在Java语言中,编译时和运行时的界线甚至更加模糊,你有足够的内省能力来列出一个类的方法,或者一直向上查询其超类链。
  Ruby无疑是现今流行语言中对元编程最友好的一种语言。没有编译时,Ruby程序中所有的语言构件在运行时可用。在运行一个程序时,无须翻过一道横亘在所写程序与所运行程序中间的墙。这里只有一个世界。 

 

C++模板元编程

STL是采用模板技术实现的

As a guideline, if any three of the following conditions apply to you, a metaprogrammed solution may be appropriate.

常见的使用场景

· You want the code to be expressed in terms of the abstractions of the problem domain. For example, you might want a parser to be expressed by something that looks like a formal grammar rather than as tables full of numbers or as a collection of subroutines; you might want array math to be written using operator notation on matrix and vector objects rather than as loops over sequences of numbers.

· You would otherwise have to write a great deal of boilerplate implementation code.

· You need to choose component implementations based on the properties of their type parameters.

· You want to take advantage of valuable properties of generic programming in C++ such as static type checking and behavioral customization, without loss of efficiency.

· You want to do it all within the C++ language, without an external tool or custom source code generator.

参考

C++模板元编程

http://product.china-pub.com/48816

C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond

By David Abrahams, Aleksey Gurtovoy

 

Ruby元编程(松本行弘作序推荐)

http://product.china-pub.com/1147651

 

 

相关文章
|
资源调度 小程序 前端开发
【微信小程序】-- npm包总结 --- 基础篇完结(四十七)
【微信小程序】-- npm包总结 --- 基础篇完结(四十七)
|
JavaScript 前端开发 Linux
pipx — 在隔离环境中安装和运行 Python 应用程序
pipx — 在隔离环境中安装和运行 Python 应用程序
478 1
|
9月前
|
机器学习/深度学习 编解码
1975 年至 2020 年全球居住人口估计值以 5 年为间隔,2025 年和 2030 年的人口预测值
该数据集描述了1975年至2020年全球居住人口的空间分布,以5年为间隔提供估计值,并预测2025年和2030年的人口分布。数据来自CIESIN GPWv4.11,基于普查数据分解到网格单元,结合GHSL全球建成区地表图层。分辨率为100米,支持研究和应用。项目由欧盟委员会和联合研究中心支持。
277 8
|
8月前
|
机器学习/深度学习 编解码 计算机视觉
RT-DETR改进策略【注意力机制篇】| CVPRW-2024 分层互补注意力混合层 H-RAMi 针对低质量图像的特征提取模块
RT-DETR改进策略【注意力机制篇】| CVPRW-2024 分层互补注意力混合层 H-RAMi 针对低质量图像的特征提取模块
129 0
|
安全 网络安全 前端开发
2023年山东省职业院校技能大赛高职组信息安全管理与评估 模块二(正式赛)
2023年山东省职业院校技能大赛高职组信息安全管理与评估 模块二(正式赛)
|
敏捷开发 Kubernetes 持续交付
阿里云云效产品使用合集之如何将流水线部署到Windows
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
|
Java Android开发
18. 【Android教程】图片控件 ImageView
18. 【Android教程】图片控件 ImageView
252 4
|
存储 缓存 关系型数据库
智能辅助驾驶业务遭遇大表瓶颈,小鹏汽车如何破局?
小鹏汽车在智能辅助驾驶业务中遇到数据库性能挑战,如大表查询慢、频繁更新和存储空间快速膨胀。他们原使用的是社区版PostgreSQL,但随着数据量增长,性能瓶颈日益凸显。为了解决这些问题,小鹏汽车采用了阿里云的PolarDB-PG。 PolarDB-PG 的存储具备弹性扩容的能力,最大可支持 100 TB 存储空间。它的大表优化和弹性跨机并行查询(ePQ),成功解决了社区 PostgreSQL 针对大表的查询和并发更新慢的问题。在小鹏汽车的智能辅助驾驶业务上,实现了每日 TB 级大数据表的 7000 万行更新和大数据表秒级分析查询。
80675 2
|
测试技术
需求不明确的情况下,测试该如何处理?
需求不明确的情况下,测试该如何处理?
369 0