template issue

Darko Miletic darko@uvcms.net
Tue Sep 14 18:20:00 GMT 2004


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