This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: compile template question
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Fri, 2 Dec 2016 15:10:28 +0000
- Subject: Re: compile template question
- Authentication-results: sourceware.org; auth=none
- References: <87504a85-f5ab-31fa-f2fd-736145f44f33@netease.com>
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;
> }
>
>
>