943,923 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 691
  • C++ RSS
Oct 21st, 2008
0

templated vector iterator trouble

Expand Post »
Hi There!

Quick, probably dumb question but I have the following snippet

C++ Syntax (Toggle Plain Text)
  1. template<typename T>
  2. class Natural : public ArbitraryNumber{
  3. std::vector<T> mVal;
  4. std::vector<T>::iterator mIter;

instantiated with

C++ Syntax (Toggle Plain Text)
  1. math::Natural<long double> z;

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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 13
Solved Threads: 6
Light Poster
seanhunt is offline Offline
40 posts
since Oct 2008
Oct 22nd, 2008
1

Re: templated vector iterator trouble

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 22nd, 2008
0

Re: templated vector iterator trouble

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>.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Oct 23rd, 2008
0

Re: templated vector iterator trouble

Thank you very much folks...just hadn't really run into that particular issue before.

Thanx...
Sean
Reputation Points: 13
Solved Threads: 6
Light Poster
seanhunt is offline Offline
40 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to counter invalid input for char
Next Thread in C++ Forum Timeline: Division flash cards





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC