This is the mail archive of the gcc@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: compile template question


N.B. This has already been answered on gcc-help, where it belongs.
Sorry for not updating this list to say so.

I've also now added this topic to the isocpp.org FAQ:
https://isocpp.org/wiki/faq/templates#dependent-name-lookup-types

On 2 December 2016 at 12:12, chenzero wrote:
> Hello,
>
> I have following code,
>
> in gcc 4.8.4, the code can NOT compile, because of an error:
> error: need ‘typename’ before ‘std::map<K, V>::iterator’ because
> ‘std::map<K, V>’ is a dependent scope
>
> however, I can compile the same code in other C++ compiler(borland c++),
>
> whether there are some differences between gcc template syntax?
>
> Thank you very much!
>
> c.z.
>
>
> // code
> //-----------------------------------------------------------
>
> #include <stdio.h>
>
> #include <map>
> #include <vector>
> using namespace std;
>
> /**
>  * get all keys of the map into vector
>  */
> template<typename K, typename V>
> static void getKeySet(std::map<K,V>& m, std::vector<K>& ks) {
>     std::map<K,V>::iterator iter = m.begin();
>     for(iter=m.begin();iter!=m.end();iter++) {
>         ks.push_back(iter->first);
>     }
> }
>
> int main() {
>     map<int,int> m1;
>     //m1[1] = 1;
>     vector<int> ks;
>     getKeySet(m1, ks);
>     printf("key size: %d\n", ks.size());
>     return 0;
> }
>
>
>


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