public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: using the member types of a parameter class in a template function
@ 2003-09-18 19:08 lrtaylor
  0 siblings, 0 replies; 3+ messages in thread
From: lrtaylor @ 2003-09-18 19:08 UTC (permalink / raw)
  To: steven.keuchel, gcc-help

Hello,

I believe your code should be like this:

01: #include <vector>
02:
03: template<class CONT> void templfunc(const CONT& mycont)
04: {
05:    typename  CONT::iterator myiter; // and now do sth with it
06: }
07:
08: int main(int argc, char **argv)
09: {
10:     std::vector<int> myvec;
11:     templfunc(myvec);
12:
13:     return 0;
14: }

Note that I have added "typename" to line 5.  I believe that using "typename" here adheres to the ANSI standard.  It just may be that MS has decided to make it optional in their compiler.  This compiles with GCC 3.3.1.  I suspect it will with 3.2.3 as well, but I can't test that.

In addition, you will probably want/need to use a const_iterator rather than an iterator since you're passing in a const reference as your parameter...

Thanks,
Lyle Taylor

-----Original Message-----
From: Steven Keuchel [mailto:steven.keuchel@freenet.de]
Sent: Thursday, September 18, 2003 2:36 PM
To: gcc-help@gcc.gnu.org
Subject: using the member types of a parameter class in a template function

Hi list,

i'm posting to this list instead of comp.lang.c++ because the following code
compiles with M$VC 7.0 but not with GCC.

I want to write a function that gets an arbitrary container and iterates
through it. I can access the member funcs of the passed object and even
static member functions of the parameter class (if it has some) but not
member types of the class.

01: #include <vector>
02:
03: template<class CONT> void templfunc(const CONT& mycont)
04: {
05:     CONT::iterator myiter; // and now do sth with it
06: }
07:
08: int main(int argc, char **argv)
09: {
10:     std::vector<int> myvec;
11:     templfunc(myvec);
12:
13:     return 0;
14: }

I know that i can implement this function by passing the range using 2
iterators, which would be better code, but that's not the solution i search
for.

gcc 3.2.3 says:
main.cpp:5: syntax error before `;' token

gcc 3.4 (20030910) says:
main.cpp: In function `void templfunc(const CONT&)':
main.cpp:5: error: expected `;'
main.cpp: In function `void templfunc(const CONT&) [with CONT =
std::vector<int, std::allocator<int> >]':
main.cpp:11:   instantiated from here
main.cpp:5: error: `CONT::iterator' names a type, but a non-type is expected

Is this a GCC bug or is my code faulty and there's yet another bug in M$VC ?

Best regards,
        Steven.

PS.: please CC me, as i'm not subscribed to the list, thx

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

* Re: using the member types of a parameter class in a template function
  2003-09-18 18:49 Steven Keuchel
@ 2003-09-18 19:09 ` Oliver Kullmann
  0 siblings, 0 replies; 3+ messages in thread
From: Oliver Kullmann @ 2003-09-18 19:09 UTC (permalink / raw)
  To: Steven Keuchel, gcc-help

On Thu, Sep 18, 2003 at 08:36:14PM +0000, Steven Keuchel wrote:
> Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm
> Precedence: bulk
> List-Unsubscribe: <mailto:gcc-help-unsubscribe-O.Kullmann=swansea.ac.uk@gcc.gnu.org>
> List-Archive: <http://gcc.gnu.org/ml/gcc-help/>
> List-Post: <mailto:gcc-help@gcc.gnu.org>
> List-Help: <mailto:gcc-help-help@gcc.gnu.org>
> Delivered-To: mailing list gcc-help@gcc.gnu.org
> From: Steven Keuchel <steven.keuchel@freenet.de>
> To: gcc-help@gcc.gnu.org
> Date: Thu, 18 Sep 2003 20:36:14 +0000
> User-Agent: KMail/1.5.3
> X-SA-Exim-Mail-From: gcc-help-return-13929-O.Kullmann=swansea.ac.uk@gcc.gnu.org
> Subject: using the member types of a parameter class in a template function
> X-Spam-Checker-Version: SpamAssassin 2.60-rc1 (1.197-2003-08-21-exp) on exim
> X-Spam-Level: 
> X-Spam-Status: No, hits=0.0 required=5.5 tests=none autolearn=no version=2.60-rc1
> X-SA-Exim-Version: 3.1 (built Fri Aug 22 12:30:01 GMT 2003)
> X-UIDL: "^J!!3W9"!EX~!!MN="!
> 
> Hi list,
> 
> i'm posting to this list instead of comp.lang.c++ because the following code 
> compiles with M$VC 7.0 but not with GCC.
> 
> I want to write a function that gets an arbitrary container and iterates 
> through it. I can access the member funcs of the passed object and even 
> static member functions of the parameter class (if it has some) but not 
> member types of the class.
> 
> 01: #include <vector>
> 02: 
> 03: template<class CONT> void templfunc(const CONT& mycont)
> 04: {
> 05: 	CONT::iterator myiter; // and now do sth with it
> 06: }
> 07: 
> 08: int main(int argc, char **argv)
> 09: {
> 10: 	std::vector<int> myvec;
> 11: 	templfunc(myvec);
> 12: 
> 13: 	return 0;
> 14: }
>

Hi,

the above code is not correct C++-code: The type CONT::iterator is a
*dependent type*, and the keyworde "typename" must be used, that is,
line 05 must be:

05: typename CONT::iterator myiter;

Oliver
 

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

* using the member types of a parameter class in a template function
@ 2003-09-18 18:49 Steven Keuchel
  2003-09-18 19:09 ` Oliver Kullmann
  0 siblings, 1 reply; 3+ messages in thread
From: Steven Keuchel @ 2003-09-18 18:49 UTC (permalink / raw)
  To: gcc-help

Hi list,

i'm posting to this list instead of comp.lang.c++ because the following code 
compiles with M$VC 7.0 but not with GCC.

I want to write a function that gets an arbitrary container and iterates 
through it. I can access the member funcs of the passed object and even 
static member functions of the parameter class (if it has some) but not 
member types of the class.

01: #include <vector>
02: 
03: template<class CONT> void templfunc(const CONT& mycont)
04: {
05: 	CONT::iterator myiter; // and now do sth with it
06: }
07: 
08: int main(int argc, char **argv)
09: {
10: 	std::vector<int> myvec;
11: 	templfunc(myvec);
12: 
13: 	return 0;
14: }

I know that i can implement this function by passing the range using 2 
iterators, which would be better code, but that's not the solution i search 
for.

gcc 3.2.3 says:
main.cpp:5: syntax error before `;' token

gcc 3.4 (20030910) says:
main.cpp: In function `void templfunc(const CONT&)':
main.cpp:5: error: expected `;'
main.cpp: In function `void templfunc(const CONT&) [with CONT = 
std::vector<int, std::allocator<int> >]':
main.cpp:11:   instantiated from here
main.cpp:5: error: `CONT::iterator' names a type, but a non-type is expected

Is this a GCC bug or is my code faulty and there's yet another bug in M$VC ?

Best regards,
	Steven.

PS.: please CC me, as i'm not subscribed to the list, thx

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

end of thread, other threads:[~2003-09-18 19:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-18 19:08 using the member types of a parameter class in a template function lrtaylor
  -- strict thread matches above, loose matches on Subject: below --
2003-09-18 18:49 Steven Keuchel
2003-09-18 19:09 ` Oliver Kullmann

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).