compile template question

chenzero chenzero@netease.com
Fri Dec 2 12:13:00 GMT 2016


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;
}





More information about the Gcc mailing list