templated vector iterator trouble

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 40
Reputation: seanhunt is an unknown quantity at this point 
Solved Threads: 6
seanhunt seanhunt is offline Offline
Light Poster

templated vector iterator trouble

 
0
  #1
Oct 21st, 2008
Hi There!

Quick, probably dumb question but I have the following snippet

  1. template<typename T>
  2. class Natural : public ArbitraryNumber{
  3. std::vector<T> mVal;
  4. std::vector<T>::iterator mIter;

instantiated with

  1. math::Natural<long double> z;

in my main module. Visual C++ dislikes this. When I put this in:

  1. typename std::vector<typename T>::iterator mIter;

...it works fine. Is this just VC being an incredible pain or is there a good reason for this? I should probably try it with another compiler but it took me too much time to 'fix' this...now I'm just irritated ;P

Thanx!
Sean
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,823
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 748
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: templated vector iterator trouble

 
1
  #2
Oct 22nd, 2008
>Visual C++ dislikes this.
Any conforming C++ compiler should dislike it.

>Is this just VC being an incredible pain or is there a good reason for this?
There's a good reason. Without going into excessive detail, because std::vector<T>::iterator is a qualified name that's dependent on the template argument T, there's an ambiguity there where the compiler could recognize the line as either a declaration or a statement and both are equally good choices. The typename keyword is for disambiguation so that you can say "yes, this really is a type".
Last edited by Narue; Oct 22nd, 2008 at 3:58 pm.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: templated vector iterator trouble

 
0
  #3
Oct 22nd, 2008
Templated classes denote incomplete types and you can specialize a particular case to have a completely different definition than the general case.

What's the guarantee that long double isn't a specialized case in the vector class that doesn't have an inner iterator class?

I'm not sure when it is resolved (most likely at compile time) but when typename is used, like Narue stated you are telling the compiler it is a type that exists within the case of vector<long double>.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 40
Reputation: seanhunt is an unknown quantity at this point 
Solved Threads: 6
seanhunt seanhunt is offline Offline
Light Poster

Re: templated vector iterator trouble

 
0
  #4
Oct 23rd, 2008
Thank you very much folks...just hadn't really run into that particular issue before.

Thanx...
Sean
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC