Saturday, December 4, 2010

Implicit Instantiation

The following is an example of 'Implicit Instantiation':
    template <typename T> T f(T &a) {return a*a;}
    int a = 1;
    int b=f(a);

When the compiler sees f(a) with f being a template, and a being an int, the compiler is able to deduce the template parameter to be int. The compiler instantiates the template automatically to f<int>. If the statement was 'int b=f<int>(a);' then this would be an example of explicit instantiation.

No comments:

Post a Comment