template issue

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux