PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

template members in eVC3

Is there anyway to create template members in eVC3?

I created a simple example which compiles and runs fine in VS.NET 2003, but gives me compilation errors in eVC3.

Code:

template <class T1> class TemplateClass
{
public:
template <class T2> int MemberTemplate();
};

template <class T1> template <class T2> int TemplateClass<T1>::MemberTemplate()
{
return 3;
}

//create object
TemplateClass<int> test;


I get these errors in eVC3:
error C2059: syntax error : ''template<''
error C2065: 'T1' : undeclared identifier

Is this just a feature that eVC3 doesn't support?
You have to define them in the class body in eVC/VC6 unfortunately, unless there's some other workaround.

Code:
template <class T1> class TemplateClass
{
public:
template <class T2> int MemberTemplate() {
return 3;
}
};
angedelamort · 11 Jul 2003, 14:52 UTC
I did not try it in EVC, but you are not supposed to declare it like that:

Code:

template <class T1> class TemplateClass
{
public:
template <class T2> int MemberTemplate();
};

template <class T1, class T2> int TemplateClass<T1>::MemberTemplate()
{
return 3;
}

//create object
TemplateClass<int> test;
I tried your example Velo, which works fine in VS.NET 2003 but I still get errors in eVC3.

Here's the modified example:
Code:
template <class T1> class TemplateClass
{
public:
template <class T2> int MemberTemplate() {
return 3;
}
};

//create object
TemplateClass<int> test;

//call function
test.MemberTemplate<double>();

I then get the following error:
error C2062: type 'double' unexpected

Any ideas?

angedelamort, your code is incorrect. Your thinking of a template class with no template members like this:
Code:

template <class T1, class T2> class TemplateClass
{
public:
int MemberTemplate();
};

template <class T1, class T2> int TemplateClass<T1, T2>::MemberTemplate()
{
return 3;
}

//create object
TemplateClass<int, double> test;
Pejo Software - Per · 11 Jul 2003, 23:15 UTC
I am not sure about what the C++ standard says nowdays. But are you supposed to be able to do that?
You can however do like this:
Code:

template <class T1> class TemplateClass
{
public:
template <class T2> void MemberTemplate(T2 parameter) {
parameter=3;
}
};

//create object
TemplateClass<int> test;

//call function
double par;
test.MemberTemplate(par);


Templates certainly is a weak spot for EVC3/VC6 and you might get unexpected errors.
You can't for example write list<vector<int>> but instead you must do typedef vector<int> IntVector and then use list<IntVector>!
Thank you so much PejoSoftware! It works perfectly now on both compilers.

I got my syntax in my first post from a book called "C++ Template: The Complete Guide" by Vandevoorde and Josuttis.

By the way, you can do this
list<vector<int>>
you just need spaces between vector and list
list< vector<int> >
Pejo Software - Per · 11 Jul 2003, 23:53 UTC
Anonymous wrote:

By the way, you can do this
list<vector<int>>
you just need spaces between vector and list
list< vector<int> >


How lame is that (crappy compiler that bothers with spaces)!
Thanks a lot for that tip!
PejoSoftware":1vbfkahb]


There is nothing crappy about it. It's the way it's supposed to be. ">>" is parsed as operator ">>", not ">" followed by ">".

All compilers work that way.
Pejo Software - Per · 12 Jul 2003, 09:13 UTC
OK! Good explanation, but it should not be that hard to write a compiler that could understand that it shouldn't be the >> operator. I am also pretty sure that I have compiled such code on a different compiler a couple of years ago. Well, good to now anyway, that I don't need to use my typedef solution.
Hmm maybe I should not complain about the M$ compiler. Recently I have been working with QNX and the compiler we used there was a lot worse.
Thierry wrote:
There is nothing crappy about it. It's the way it's supposed to be. ">>" is parsed as operator ">>", not ">" followed by ">".

All compilers work that way.


So why isn't < parsed as a standard < operator? I don't think operators should have anything to do with templates.
The holy standard specifies that parsing should eat as many characters as possible when generating tokens. "<" is parsed as a "<" token. ">>" is parsed as a ">>" token.

Later on, the sementic analyser will differentiate "<" used in a template syntax from an operator "<". It can do so using the context.

When the sementic analyser see ">>" it is already too late. ">>" will then be interpreted as an operator as there is no other possibility. The sementic phase is not allowed to split the token into ">" + ">".

Any compiler that doesn't work that way is not standard compliant. It is a really known "problem", just live with it.