What is monomorphisation with context to C++?

2019-01-10 18:34发布

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?

标签: c++ rust
3条回答
We Are One
2楼-- · 2019-01-10 19:10

Not sure if anyone is still looking at this, but the rust documentation actually does mention how it achieves no cost abstraction through this process:

https://doc.rust-lang.org/book/2018-edition/ch10-01-syntax.html

查看更多
倾城 Initia
3楼-- · 2019-01-10 19:13

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"));

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.

查看更多
贼婆χ
4楼-- · 2019-01-10 19:13

Not sure about this; could you link to the talk? It might have been an offhanded remark.

Herman might have coined a term for something like template specialization, which generates types/objects which are mutually unrelated (not-polymorphic or "monomorphic") from the template, which is a polymorphic structure.

查看更多
登录 后发表回答