Compiles with GCC 4.5.0:
Code:
#include <iostream>
using namespace std;
template <typename T, unsigned int N>
class Stack;
template<typename T, unsigned int N>
ostream &operator<<(ostream &os, const Stack<T, N> &s)
{
if (!s.empty())
{
T *cur = s.m_cur;
while (cur != s.m_stack)
{
os << *--cur << ' ';
}
}
return os;
}
template <typename T, unsigned int N>
class Stack
{
public:
friend ostream &operator<< <>(ostream &os, const Stack<T, N> &s);
private:
T m_stack[N];
};
int main()
{
Stack<int, 10> stack;
}
When you take out the <> you get these errors:
/home/daemn/Temporary/asdasdasda/main4.cpp|26|warning: friend declaration ‘std:
stream& operator<<(std:
stream&, const Stack<T, N>&)’ declares a non-template function|
/home/daemn/Temporary/asdasdasda/main4.cpp|26|note: (if this is not what you intended, make sure the function template has already been declared and
add <> after the function name here) |
/home/daemn/Temporary/asdasdasda/main4.cpp||In function ‘int main()’:|
/home/daemn/Temporary/asdasdasda/main4.cpp|34|warning: unused variable ‘stack’|
||=== Build finished: 1 errors, 2 warnings ===|
So it's basically short-hand?