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::iterator’ because > ‘std::map ’ 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 > > #include > #include > using namespace std; > > /** > * get all keys of the map into vector > */ > template > static void getKeySet(std::map & m, std::vector& ks) { > std::map ::iterator iter = m.begin(); > for(iter=m.begin();iter!=m.end();iter++) { > ks.push_back(iter->first); > } > } > > int main() { > map m1; > //m1[1] = 1; > vector ks; > getKeySet(m1, ks); > printf("key size: %d\n", ks.size()); > return 0; > } > > >
Re: compile template question
On 12/02/2016 07:12 AM, 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::iterator’ because ‘std::map ’ 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? Your code requires the typename. See 14.6.2 of the std for reasons. nathan -- Nathan Sidwell
compile template question
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::iterator’ because ‘std::map ’ 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 #include #include using namespace std; /** * get all keys of the map into vector */ template static void getKeySet(std::map & m, std::vector& ks) { std::map ::iterator iter = m.begin(); for(iter=m.begin();iter!=m.end();iter++) { ks.push_back(iter->first); } } int main() { map m1; //m1[1] = 1; vector ks; getKeySet(m1, ks); printf("key size: %d\n", ks.size()); return 0; }