Why is a member function not automatically recognized as a function template?

C ++ faq 35.16

http://www.parashift.com/c++-faq-lite/template-friends.html

#include <iostream>

template<typename T>
class Foo {
public:
  Foo(T const& value = T());
  friend Foo<T> operator+ (const Foo<T>& lhs, const Foo<T>& rhs);
  friend std::ostream& operator<< (std::ostream& o, const Foo<T>& x);
private:
  T value_;
};

The author claims:

'Capture occurs when the compiler sees the lines of friends in the correct class definition. At this moment, he still does not know that the friend’s functions are templates themselves (why this? Are the template template functions not the default template?) ; he suggests that they are not patterns: "

Foo<int> operator+ (const Foo<int>& lhs, const Foo<int>& rhs)
{ ... }  

std::ostream& operator<< (std::ostream& o, const Foo<int>& x)
{ ... }

Why are the above not templates? are these patterns that are created via int?

' + < < , , , "undefined " , .

, , , :

template<typename T> class Foo;  // pre-declare the template class itself
template<typename T> Foo<T> operator+ (const Foo<T>& lhs, const Foo<T>& rhs);
template<typename T> std::ostream& operator<< (std::ostream& o, const Foo<T>& x);

- ? , Foo, T "int" .

.

+5
1

, . :

struct S {
    friend void foo(S);
};

, void foo(S) ; friend , void foo(S), S. , .

:

template<typename T> struct S {
    friend void foo(S);
};

, T, void foo(S<T>), S<T>. , , :

void foo(S<char>) { }
void foo(S<int>) { }

, , T. , , , , .

, - , , "" . :

#include <iostream>
template<typename T> struct S;
template<typename T> void foo(S<T>);
template<typename T> void foo(S<T *>);
template<typename T> struct S {
    friend void foo<>(S);
};
template<typename T> void foo(S<T>) { std::cout << "template template friend\n"; }
template<typename T> void foo(S<T *>) { std::cout << "template specialization template friend\n"; }
template void foo(S<void *>);
int main() {
    foo(S<int>());
    foo(S<void *>());
}

foo, , friend .

+5

All Articles