This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Re: compile template question


It is the evilness of C.

If the code without `typename` were valid it would be ambiguous, because
`std::map<K,V>::iterator` could well be the name of some static member variable
followed by a less-than operator and what you think is a declaration
looks like an expression statement in spite that in no case is it valid.
MSVC defers the distinguishment to template instantiation time
(in reality, MSVC doesn't do the first phase of name lookup at all,
hence violates the standard) at which time such ambiguity is gone.

------------------				 
Best regards,
lh_mouse
2016-12-02

-------------------------------------------------------------
发件人:chenzero <chenzero@netease.com>
发送日期:2016-12-02 22:03
收件人:Jonathan Wakely
抄送:gcc-help
主题:Re: compile template question

On 2016年12月02日 21:34, Jonathan Wakely wrote:
> On 2 December 2016 at 13:10, chenzero wrote:
>> currently, I am confused because the same code can be compiled in C++Builder
>> and Visual studio 2008, but not gcc.
> So read the link, it explains the problem.
>
> The code is not valid according to the C++ standard, so GCC is
> correct, and your older Borland and Visual Studio compilers are wrong.

Thanks a lot for your help!

the standard way for code is:  add a "typename" before the iter declaration.

template<typename K, typename V>
static void getKeySet(std::map<K,V>& m, std::vector<K>& ks) {
     typename std::map<K,V>::iterator iter;
     for(iter=m.begin();iter!=m.end();iter++) {
         ks.push_back(iter->first);
     }
}





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]