public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* compile template question
@ 2016-12-02 12:13 chenzero
  2016-12-02 15:07 ` Nathan Sidwell
  2016-12-02 15:10 ` Jonathan Wakely
  0 siblings, 2 replies; 3+ messages in thread
From: chenzero @ 2016-12-02 12:13 UTC (permalink / raw)
  To: gcc

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



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: compile template question
  2016-12-02 12:13 compile template question chenzero
@ 2016-12-02 15:07 ` Nathan Sidwell
  2016-12-02 15:10 ` Jonathan Wakely
  1 sibling, 0 replies; 3+ messages in thread
From: Nathan Sidwell @ 2016-12-02 15:07 UTC (permalink / raw)
  To: chenzero, gcc

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<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?

Your code requires the typename. See 14.6.2 of the std for reasons.

nathan

-- 
Nathan Sidwell

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: compile template question
  2016-12-02 12:13 compile template question chenzero
  2016-12-02 15:07 ` Nathan Sidwell
@ 2016-12-02 15:10 ` Jonathan Wakely
  1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2016-12-02 15:10 UTC (permalink / raw)
  To: gcc

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-12-02 15:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-02 12:13 compile template question chenzero
2016-12-02 15:07 ` Nathan Sidwell
2016-12-02 15:10 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).