What is monomorphisation with context to C++?

前端 未结 4 1842
孤独总比滥情好
孤独总比滥情好 2020-11-30 23:00

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 monomorphisat

4条回答
  •  一生所求
    2020-11-30 23:28

    Monomorphization means generating specialized versions of generic functions. If I write a function that extracts the first element of any pair:

    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.

提交回复
热议问题