To make Sebastiani's example a little more fancy with lovely C++0x functionality:
Code:
template<typename K>
class Foo
{
typedef typename K::iterator iterator;
typedef typename K::const_iterator const_iterator;
K container;
public:
Foo(const K& container)
: container(container)
{
typedef decltype(*container.begin()) value_type;
}
};
int main()
{
// No space between ">>" needed in C++0x.
std::vector<int> vec;
Foo<std::vector<int>> foo_vec(vec);
std::list<int> list;
Foo<std::list<int>> foo_list(list);
return 0;
}
And I'll also tell you a remember rule for typename. So long as you are using a dependent type, you must use typename. A dependent type is a type which relies on some unknown type T. To know WTH I'm talking about, here are some examples:
Code:
template<typename T> struct bar { typedef int myint; };
template<typename T>
void foo()
{
int MyVar1; // Not a dependent name. Obviously.
typename bar<T>::myint MyVar2; // Dependant name, because we are accessing myint inside bar which depends on T which is unknown.
bar<int>::myint MyVar3; // Not a dependent name because int is a known type.
typename std::vector<T>::iterator MyIt1; // Dependent name because T is unknown.
std::vector<char>::iterator MyIt2; // Not dependent name because char is a known type.
}