patterncppCritical
What is monomorphisation with context to C++?
Viewed 0 times
withmonomorphisationwhatcontext
Problem
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?
Solution
Monomorphization means generating specialized versions of generic functions. If I write a function that extracts the first element of any pair:
and then I call this function twice:
The compiler will generate two versions of
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.
fn first(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.
Code Snippets
fn first<A, B>(pair: (A, B)) -> A {
let (a, b) = pair;
return a;
}first((1, 2));
first(("a", "b"));Context
Stack Overflow Q#14189604, score: 166
Revisions (0)
No revisions yet.