public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: template issue
@ 2004-09-14 18:37 lrtaylor
  0 siblings, 0 replies; 3+ messages in thread
From: lrtaylor @ 2004-09-14 18:37 UTC (permalink / raw)
  To: darko, gcc-help

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: template issue
  2004-09-14 18:20 Darko Miletic
@ 2004-09-14 18:38 ` Eljay Love-Jensen
  0 siblings, 0 replies; 3+ messages in thread
From: Eljay Love-Jensen @ 2004-09-14 18:38 UTC (permalink / raw)
  To: Darko Miletic, gcc help

Hi Darko,

In this situation, GCC is compliant with ISO 14882 specification.  BCB 5&6 
and MSVC 7.1 are not.

The typename keyword is required because the compiler is NOT supposed to 
presume that T::value_type is a type in that context, unless annotated with 
typename.

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 3+ messages in thread

* template issue
@ 2004-09-14 18:20 Darko Miletic
  2004-09-14 18:38 ` Eljay Love-Jensen
  0 siblings, 1 reply; 3+ messages in thread
From: Darko Miletic @ 2004-09-14 18:20 UTC (permalink / raw)
  To: gcc help

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-09-14 18:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-14 18:37 template issue lrtaylor
  -- strict thread matches above, loose matches on Subject: below --
2004-09-14 18:20 Darko Miletic
2004-09-14 18:38 ` Eljay Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).