| | |
templated vector iterator trouble
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 40
Reputation:
Solved Threads: 6
Hi There!
Quick, probably dumb question but I have the following snippet
instantiated with
in my main module. Visual C++ dislikes this. When I put this in:
...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
Quick, probably dumb question but I have the following snippet
C++ Syntax (Toggle Plain Text)
template<typename T> class Natural : public ArbitraryNumber{ std::vector<T> mVal; std::vector<T>::iterator mIter;
instantiated with
C++ Syntax (Toggle Plain Text)
math::Natural<long double> z;
in my main module. Visual C++ dislikes this. When I put this in:
C++ Syntax (Toggle Plain Text)
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
>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".
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
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>.
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>.
![]() |
Other Threads in the C++ Forum
- Previous Thread: How to counter invalid input for char
- Next Thread: Division flash cards
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






