Saturday, December 4, 2010

Explicit Specialization and Explicit Instantiation

The following does not compile:
    template <typename T> T f(T &a);
    template <> int f(int &a);
    template <> f<int>(int &a);
    int a = 1;
    int b=f(a);

The standard says it is not permitted to have a function with Explicit Specialization and Explicit Instantiation in the same compilation unit. Otherwise,

the function to use would be ambiguous for the compiler.

1 comment:

  1. These are two alternate syntaxes for specialization:
    template <> int f(int &a);
    template <> f(int &a);

    An instantiation would have the syntax:
    template int f(int &a);

    Note the lack of empty <> template braces for the instantiation.

    Also note that instantiation *after* specialization within the same TU is OK, but the converse will not compile.

    ReplyDelete