Highlighting member function in a class template

Why can’t I Tdeduce from the fnsignature in the following example?

template<int I>
struct Class {
    template<typename T>
    Class &operator <<( void (*)(const Class<I> &, const T &) ) { return *this; }
};

struct Ot { };

template<int I>
void fn(const Class<I> &, const Ot &) { }

int main() {
    Class<1>() << fn;
}

If, on the contrary, the following example without a regular member operator<<is legal:

template<int I>
struct Class {
    Class &operator <<( void (*)(const Class<I> &) ) { return *this; }
};

struct Ot { };

template<int I>
void fn(const Class<I> &) { }

int main() {
    Class<1>() << fn;
}
+6
source share
2 answers

What is the matter, that the template parameters for the function template can either be explicitly set, or they can be deduced from the arguments passed to the function.

fn, Class<1>::operator<<() . : fn<1> ( , ). , , fn , T Class<1>::operator() (.. Fn < 1 > ).

, fn . fn < 2 > Class < 1 > :: operator < (lt)(). :

Class<1> << fn<2>;   // fn<2> is not fn<1>
Class<1> << fn<1>;   // OK too, of course
Class<1> << fn;      // error! fn<?>
0

, Class<1> Class, fn:

Class<1>() << fn<1>;

, operator <<:

template<typename T>
Class & operator <<( void (*)(const Class<I> &, const T &) ) { return *this; }
      ^
+2

All Articles