typename usege problem ...

Discussion in 'C++' started by 466, Nov 3, 2006.

  1. 466

    466 Guest

    Hi all,
    I have this member in a template class:

    bool opsInsert(int nId, T pObj){
    pairRet = insert(typename std::map<int,T>::value_type(nId, pObj)); }

    which does compile with gcc but not with cl.exe (Windows).
    If I remove the typename keyword, the code does not compile with gcc,
    but compiles witc cl.exe....

    The question: which is the standard way to call this function? Is there
    any workaround to make compile with both compilers?

    I know this is a compiler issue, but I hope you will help me ...

    thanx,
    stefan
    466, Nov 3, 2006
    #1
    1. Advertising

  2. 466

    Rolf Magnus Guest

    466 wrote:

    > Hi all,
    > I have this member in a template class:
    >
    > bool opsInsert(int nId, T pObj){
    > pairRet = insert(typename std::map<int,T>::value_type(nId, pObj)); }
    >
    > which does compile with gcc but not with cl.exe (Windows).
    > If I remove the typename keyword, the code does not compile with gcc,
    > but compiles witc cl.exe....
    >
    > The question: which is the standard way to call this function?


    The standard requires the typename keyword in this place.

    > Is there any workaround to make compile with both compilers?


    Maybe use a macro, like:

    #if compiler is vc++
    #define TYPENAME
    #else
    #define TYPENAME typename
    #endif

    bool opsInsert(int nId, T pObj){
    pairRet = insert(TYPENAME std::map<int,T>::value_type(nId, pObj)); }

    It's ugly, but works.
    Rolf Magnus, Nov 3, 2006
    #2
    1. Advertising

  3. 466

    466 Guest

    Rolf Magnus wrote:
    > 466 wrote:
    >
    > > Hi all,
    > > I have this member in a template class:
    > >
    > > bool opsInsert(int nId, T pObj){
    > > pairRet = insert(typename std::map<int,T>::value_type(nId, pObj)); }
    > >
    > > which does compile with gcc but not with cl.exe (Windows).
    > > If I remove the typename keyword, the code does not compile with gcc,
    > > but compiles witc cl.exe....
    > >
    > > The question: which is the standard way to call this function?

    >
    > The standard requires the typename keyword in this place.
    >
    > > Is there any workaround to make compile with both compilers?

    >
    > Maybe use a macro, like:
    >
    > #if compiler is vc++
    > #define TYPENAME
    > #else
    > #define TYPENAME typename
    > #endif
    >
    > bool opsInsert(int nId, T pObj){
    > pairRet = insert(TYPENAME std::map<int,T>::value_type(nId, pObj)); }
    >
    > It's ugly, but works.


    Sure, as a ultimate solution ... :(
    Thanx.
    466, Nov 3, 2006
    #3
  4. 466

    Ron Natalie Guest

    466 wrote:
    > Hi all,
    > I have this member in a template class:
    >
    > bool opsInsert(int nId, T pObj){
    > pairRet = insert(typename std::map<int,T>::value_type(nId, pObj)); }
    >
    > which does compile with gcc but not with cl.exe (Windows).
    > If I remove the typename keyword, the code does not compile with gcc,
    > but compiles witc cl.exe....
    >

    Get a version of the windows compiler that was published in the last
    five years.
    Ron Natalie, Nov 3, 2006
    #4
  5. 466

    jim_taylor Guest


    > Maybe use a macro, like:
    >
    > #if compiler is vc++
    > #define TYPENAME
    > #else
    > #define TYPENAME typename
    > #endif
    >


    You could use the macro
    #define typename
    as basically the older compilers don't know the typename keyword.
    Be careful to make your #if specific to the current (and older)
    versions of your compiler, as you don't want it to break when you do
    upgrade...
    Note also that the standard also allows typename as a synonym
    (basically) for class in the template arguments,which also will not
    work in yuour old compiler. i.e. you can write
    template <typename MyType>
    jim_taylor, Nov 3, 2006
    #5
  6. 466

    Bo Persson Guest

    jim_taylor wrote:
    >> Maybe use a macro, like:
    >>
    >> #if compiler is vc++
    >> #define TYPENAME
    >> #else
    >> #define TYPENAME typename
    >> #endif
    >>

    >
    > You could use the macro
    > #define typename
    > as basically the older compilers don't know the typename keyword.


    Except that in this case we recognize a certain compiler, that requires
    typename in some places, doesn't care in other places, and flat out refuses
    to recognize some cases where typename *is* required.

    A very useful advice - use a compiler from this millennium - has alredy been
    given by another poster.


    Bo Persson
    Bo Persson, Nov 3, 2006
    #6
  7. 466

    Mark P Guest

    466 wrote:
    > Hi all,
    > I have this member in a template class:
    >
    > bool opsInsert(int nId, T pObj){
    > pairRet = insert(typename std::map<int,T>::value_type(nId, pObj)); }
    >
    > which does compile with gcc but not with cl.exe (Windows).
    > If I remove the typename keyword, the code does not compile with gcc,
    > but compiles witc cl.exe....
    >
    > The question: which is the standard way to call this function? Is there
    > any workaround to make compile with both compilers?
    >
    > I know this is a compiler issue, but I hope you will help me ...
    >
    > thanx,
    > stefan
    >


    As has been noted, the standard requires "typename". However you could
    replace the expression by the actual type of value_type, namely
    std::pair< const int, T>. That should not require "typename".
    Mark P, Nov 4, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jakob Bieling

    Q: typename or not typename?

    Jakob Bieling, Mar 14, 2006, in forum: C++
    Replies:
    2
    Views:
    354
    Rolf Magnus
    Mar 14, 2006
  2. xuatla

    implicit typename problem

    xuatla, Jul 19, 2006, in forum: C++
    Replies:
    8
    Views:
    373
    Howard Gardner
    Jul 20, 2006
  3. StephQ

    Problem with typename.

    StephQ, May 16, 2007, in forum: C++
    Replies:
    7
    Views:
    338
    StephQ
    May 22, 2007
  4. Carramba

    program resource usege

    Carramba, Jun 28, 2007, in forum: C Programming
    Replies:
    2
    Views:
    305
    Eric Sosman
    Jun 28, 2007
  5. Replies:
    3
    Views:
    270
    Zhihao Yuan
    Nov 20, 2012
Loading...

Share This Page