template issue
lrtaylor@micron.com
lrtaylor@micron.com
Tue Sep 14 18:37:00 GMT 2004
Other compilers may not require it, but it is in accordance with the
ANSI standard. I don't know where it talks about it in the standard,
but you can look in Stroustrup's "The C++ Programming Language" 3rd
edition, Appendix C, section 13.5. He gives a good explanation why this
is necessary. Perhaps someone else on the list can actually explain why
this is so, if you don't have access to a good C++ reference.
Thanks,
Lyle
-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Darko Miletic
Sent: Tuesday, September 14, 2004 12:20 PM
To: gcc help
Subject: template issue
I have been playing with std::for_each and std::map and came with
interresting issue:
//main.cpp
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
typedef std::map<int, std::string> mapType;
template <typename M>
inline void printMapValue(const M& value) {
std::cout << value.first << " - "
<< value.second << "\n";
}
template <typename T >
void print_map(const T& map_) {
std::for_each( map_.begin(),
map_.end(),
printMapValue<typename T::value_type>); // if
typename is removed here GCC reports error???
std::cout << std::endl;
}
int main(int argc, char* argv[])
{
mapType map_;
map_[0] = "111";
map_[1] = "222";
map_[2] = "333";
print_map<mapType>(map_);
return 0;
}
If I remove keyword typename from print_map function GCC reports this:
error: no matching function for call to `for_each(
std::_Rb_tree_iterator<std::pair<const int, std::string>, const
std::pair<const int, std::string>&, const std::pair<const int,
std::string>*>, std::_Rb_tree_iterator<std::pair<const int,
std::string>,
const std::pair<const int, std::string>&, const std::pair<const int,
std::string>*>, <unknown type>)'
Why is typename needed here? Other compilers like BCB 5&6 and MSVC 7.1
compile this fine without typename keyword.
Darko
More information about the Gcc-help
mailing list