Why can't the C ++ compiler infer the template parameters?

Consider the following example:

template<typename TI>
char trunc(TI IN){
        return (char)IN;
}

template <typename TO, typename TI>
TO applyf(TO (OP)(TI), TI IN){
        return OP(IN);
}

template <typename TO, typename TI,
          TO (OP)(TI)>
TO applyt(TI IN){
        return OP(IN);
}

int main(){
        int i = -21;
        char r1 = applyf(trunc<int>, i);
        char r2 = applyt<char, int, trunc>(i);
        char r3 = applyt<trunc>(i);
}

      

When I compile this code in g ++ (since C ++ 11) I get errors:

.Cpp function: 21:12: error: no matching function to call 'applyt'

   char r3 = applyt<trunc>(i);

      

Function.cpp: 13: 4: note: candidate pattern ignored: invalid explicit argument for pattern parameter "TO" TO applyt (TI IN) {

So my question is simple: the type of the input arguments is obvious - int - and TO should be obvious to me.

Why can't TO and TI be outputted, but they can be outputted to r1?

+3


source to share


1 answer


trunc

is a function template, not a type, so it cannot match typename TO

.



+4


source







All Articles