Where and why do you need to put the template and typename keywords in C++?
Hello colleagues! I've been writing in C++ for a few months now, but templates still give me a headache. Especially confusing are the `template` and `typename` keywords. I understand they are somehow related to dependent names, but in practice I keep getting compilation errors.
Here's a specific example that broke me. Suppose I have a template container class, and I want to write a function that prints elements from a nested iterator type:
```cpp
#include
#include
template
void printFirst(const T& container) {
T::const_iterator it = container.begin(); // Error!
std::cout << *it << std::endl;
}
int main() {
std::vector vec = {1, 2, 3};
printFirst(vec);
return 0;
}
```
The compiler (G
No answers yet.
Log in to leave an answer