This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
compile template question
- From: chenzero <chenzero at netease dot com>
- To: gcc at gcc dot gnu dot org
- Date: Fri, 2 Dec 2016 20:12:48 +0800
- Subject: compile template question
- Authentication-results: sourceware.org; auth=none
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;
}