views:

270

answers:

3

I have a template class compiling fine in VC6. But it is not compiling in VC9. For that I have added "typename" keyword in front of one type. After this it is compiling in vc9 but not in VC6. How do I make it compiling with both the compilers.

#include <map> 
#include <vector>

template <class T1, class T2>
class A
{
public:
    std::pair<typename std::vector<std::pair<T1,T2> >::iterator, bool > insert(const std::pair<T1 ,T2> &value_in);
};

template <class T1, class T2>
std::pair< std::vector<std::pair<T1,T2> >::iterator, bool> A<T1,T2>::insert(const std::pair<T1 ,T2>& value_in)
{
    std::pair< std::vector<std::pair<T1,T2> >::iterator, bool> b;
    return b;
}

void main()
{
    A<int, int> a;
    a.insert(std::pair<int, int>(0, 0));
}

Above code compiles in VC6. But not in VC9. Following code works in VC9, not in VC6.

#include <map> 
#include <vector>

template <class T1, class T2>
class A
{
public:
    std::pair<typename std::vector<std::pair<T1,T2> >::iterator, bool > insert(const std::pair<T1 ,T2> &value_in);
};

template <class T1, class T2>
std::pair<typename std::vector<std::pair<T1,T2> >::iterator, bool> A<T1,T2>::insert(const std::pair<T1 ,T2>& value_in)
{
    std::pair< std::vector<std::pair<T1,T2> >::iterator, bool> b;
    return b;
}

void main()
{
    A<int, int> a;
    a.insert(std::pair<int, int>(0, 0));
}
+5  A: 

VC6 is not very standards-compliant. You may just have to #IFDEF your way around this one.

Joe
+1  A: 

I do not have VC6, but VC2003 complains too. So i put a typename before

std::pair< typename std::vector<std::pair<T1,T2> >::iterator, bool > insert(const std::pair<T1 ,T2> &value_in);

to make it

typename std::pair< typename std::vector<std::pair<T1,T2> >::iterator, bool > insert(const std::pair<T1 ,T2> &value_in);

And it worked, maybe you can try it too. HTH,

Abhay
This didn't worked
Shino C G
Curiously Comeau too complains. Try putting the typename before use of std::vector<std::pair<T1,T2> > like std::pair<typename std::vector<std::pair<T1,T2> >::iterator, bool> b; Them Comeau accepts.
Abhay
+1  A: 

Changes commented:

#include <map>
#include <vector>

template <class T1, class T2>
class A
{
public:
    std::pair<typename std::vector<std::pair<T1,T2> >::iterator, bool > insert(const std::pair<T1 ,T2> &value_in);
};

template <class T1, class T2>
std::pair< typename std::vector<std::pair<T1,T2> >::iterator, bool> // +typename
A<T1,T2>::insert(const std::pair<T1 ,T2>& value_in)
{
    std::pair< typename std::vector<std::pair<T1,T2> >::iterator, bool> b; // +typename
    return b;
}

int main() // void->int
{
    A<int, int> a;
    a.insert(std::pair<int, int>(0, 0));
}
The two typenames make it correct C++. Use this one as your 'main', adapt with some #ifdefs for VC6 specifically if you really need to imho.
Pieter