Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.straub-nv.de!news-1.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Ian Collins Newsgroups: comp.lang.c++ Subject: Re: Defining iterator type through container object type Date: Thu, 15 Sep 2011 10:25:31 +1200 Lines: 37 Message-ID: <9dcnusF786U1@mid.individual.net> References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net N4Vf7yjkvdxnAUKtPShbpAxMt9nJA7S3ew6EMiSOSpGskB5Bvz Cancel-Lock: sha1:yJYgV/eWbUAWpfZHWrZjf9azFUc= User-Agent: Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4 In-Reply-To: Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++:10111 On 09/15/11 09:51 AM, Urs Thuermann wrote: > Can I define an iterator for an object of a standard container class > without using the concrete type of that container object? > > I tried things like > > 1 #include > 2 void foo(std::list ilist) { > 3 ilist.iterator it; > 4 for (it = ilist.begin(); it != ilist.end(); ++it) { *it; } > 5 } > > and > > 1 #include > 2 void foo(std::list ilist) { > 3 typeof(ilist)::iterator it; > 4 for (it = ilist.begin(); it != ilist.end(); ++it) { *it; } > 5 } > > For the first code I get > > foo.cc:3:11: error: invalid use of 'std::list::iterator' > > so at least ilist.iterator seems indeed to be understood as > std::list::iterator. In the second case I get > > foo.cc:3:29: error: expected initializer before 'it' > > Is there a way to define the iterator without using std::list::iterator > explicitly? Only if your compiler supports the new auto keyword. Otherwise use a typedef for the parameter type. -- Ian Collins