typename usege problem ...

4

466

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
 
R

Rolf Magnus

466 said:
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.
 
4

466

Rolf said:
The standard requires the typename keyword in this place.


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.
 
R

Ron Natalie

466 said:
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.
 
J

jim_taylor

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

Bo Persson

jim_taylor said:
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
 
M

Mark P

466 said:
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".
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top