What is monomorphisation with context to C++?(什么是带有 C++ 上下文的单态化?)
问题描述
Dave Herman 最近在 Rust 的演讲说他们从 C++ 借用了这个属性.我找不到有关该主题的任何内容.有人能解释一下什么是单态化吗?
Dave Herman's recent talk in Rust said that they borrowed this property from C++. I couldn't find anything around the topic. Can somebody please explain what monomorphisation means?
推荐答案
单态化意味着生成通用函数的特殊版本.如果我编写一个函数来提取任何对的第一个元素:
Monomorphization means generating specialized versions of generic functions. If I write a function that extracts the first element of any pair:
fn first<A, B>(pair: (A, B)) -> A {
let (a, b) = pair;
return a;
}
然后我调用了这个函数两次:
and then I call this function twice:
first((1, 2));
first(("a", "b"));
编译器将生成两个版本的 first(),一个专门用于整数对,一个专门用于字符串对.
The compiler will generate two versions of first(), one specialized to pairs of integers and one specialized to pairs of strings.
该名称源自编程语言术语多态性"——意思是一种可以处理多种类型数据的函数.单态化是从多态到单态代码的转换.
The name derives from the programming language term "polymorphism" — meaning one function that can deal with many types of data. Monomorphization is the conversion from polymorphic to monomorphic code.
这篇关于什么是带有 C++ 上下文的单态化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么是带有 C++ 上下文的单态化?
基础教程推荐
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
