本文介绍了什么是C ++上下文的单态化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,他们是从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 ++上下文的单态化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 17:17