C++ How do compilers handle templates

后端 未结 3 1230
孤城傲影
孤城傲影 2021-01-20 19:56

As some of you may know from my recent posts i am studying for a C++ exam which the content for the class was delivered very poorly. I am basically having to self teach ever

3条回答
  •  遇见更好的自我
    2021-01-20 20:26

    let's say you write a function using templates:

    template 
    void function(T t){
     doSomething();
    }
    

    for each data type you call this function, the compiler simply replaces the 'T' with that data type, say 'int' and generates code for that like you've written this function with 'int' instead of 'T' since the beginning. This is probably the right (but not the complete) answer if others agreed.

提交回复
热议问题