problem: calling a template member function

J

john.constantine

Hi
I have this code:

[begin main.cpp]
template<typename ClassType>
struct S
{
template<typename FunctionType> void member() {};
};

template<typename Type>
void g()
{
S<Type> s;
/*line 11*/ s.member<Type>();
}

void h()
{
S<int> s;
s.member<int>();
}

int main(int, char**)
{
g<int>();
h();
return 0;
}
[end main.cpp]

I compile it with g++ (GCC) 3.4.2 (mingw-special) and get this compiler
error:
.../main.cpp: In function `void g()':
.../main.cpp:11: error: expected primary-expression before '>' token
.../main.cpp:11: error: expected primary-expression before ')' token

I do not understand what is wrong with the expression in Line 11 and
where the difference between the calls in g and h is.

Can anyone explain this?

TIA
Gabriel
 
F

Frank Chang

Gabriel, I took your code and compiled it with MSVC 7.1 , which
increases conformance with C++ standard. It compiled and ran OK. I know
MSVC7.1 and g++ 3.4.2 are different but don't both compilers try to
conform with C++ standard? Thank you
 
G

Gabriel

Frank said:
Gabriel, I took your code and compiled it with MSVC 7.1 , which
increases conformance with C++ standard. It compiled and ran OK. I know
MSVC7.1 and g++ 3.4.2 are different but don't both compilers try to
conform with C++ standard? Thank you

Thank you, Frank

Yes, I think that they both try to conform to the standard. And I think
g++ 3.4 (or later) is very close to the standard. As far as I understand
the language, my code should be correct, but I am not sure if the g++
should translate it or if the MSVC makes a mistake by translating it.

If it's a problem with the g++, does anyone know a workaround?
 
G

Gabriel

Some additional information:

I just ran it under the IAR Embedded Workbench IDE with the MSP430 IAR
C/C++ Compiler, and it tells me

Error[Pe254]: type name is not allowed H:\Projects\Test (IAR Embedded
Workbench IDE)\main.cpp 11
Error[Pe029]: expected an expression H:\Projects\Test (IAR Embedded
Workbench IDE)\main.cpp 11

Gabriel
 
A

annamalai.gurusami

Gabriel said:
If it's a problem with the g++, does anyone know a workaround?

FWIW, The code that you posted doesn't compile with g++ 3.3.4 as well.
Anyway, as a work around you can add a dummy parameter to the
templated member function.

template<typename ClassType>
struct S
{
template<typename FunctionType> void member(FunctionType* t =0) {};
};

template<typename Type>
void g()
{
S<Type> s;
s.member( static_cast<Type*>(0) );
}

Rgds,
anna
 
V

Victor Bazarov

john.constantine said:
Hi
I have this code:

[begin main.cpp]
template<typename ClassType>
struct S
{
template<typename FunctionType> void member() {};

Drop the trailing semicolon: while it is not a severe violation,
it is entirely superfluous and a bad habit.
};

template<typename Type>
void g()
{
S<Type> s;
/*line 11*/ s.member<Type>();

Needs to be
s.template member<Type>();

to be standard C++. MS compiler hasn't got there yet.
}

void h()
{
S<int> s;
s.member<int>();
}

int main(int, char**)
{
g<int>();
h();
return 0;
}
[end main.cpp]

I compile it with g++ (GCC) 3.4.2 (mingw-special) and get this compiler
error:
../main.cpp: In function `void g()':
../main.cpp:11: error: expected primary-expression before '>' token
../main.cpp:11: error: expected primary-expression before ')' token

I do not understand what is wrong with the expression in Line 11 and
where the difference between the calls in g and h is.

Can anyone explain this?

A template member of a template needs to be specified as such to help
the compiler resolve the name. I don't remember the exact paragraph
of the Standard, but you could search the archives on Google, it has
come up more than once in this newsgroup.

V
 
G

Gabriel

FWIW, The code that you posted doesn't compile with g++ 3.3.4 as well.
Anyway, as a work around you can add a dummy parameter to the
templated member function.

template<typename ClassType>
struct S
{
template<typename FunctionType> void member(FunctionType* t =0) {};
};

template<typename Type>
void g()
{
S<Type> s;
s.member( static_cast<Type*>(0) );
}

Rgds,
anna

That's a nifty solution! I think that will help me out for now...
Thanx anna!

Gabriel
 
G

Gabriel

Victor said:
john.constantine said:
Hi
I have this code:

[begin main.cpp]
template<typename ClassType>
struct S
{
template<typename FunctionType> void member() {};


Drop the trailing semicolon: while it is not a severe violation,
it is entirely superfluous and a bad habit.
};

template<typename Type>
void g()
{
S<Type> s;
/*line 11*/ s.member<Type>();


Needs to be
s.template member<Type>();

to be standard C++. MS compiler hasn't got there yet.
}

void h()
{
S<int> s;
s.member<int>();
}

int main(int, char**)
{
g<int>();
h();
return 0;
}
[end main.cpp]

I compile it with g++ (GCC) 3.4.2 (mingw-special) and get this
compiler error:
../main.cpp: In function `void g()':
../main.cpp:11: error: expected primary-expression before '>' token
../main.cpp:11: error: expected primary-expression before ')' token

I do not understand what is wrong with the expression in Line 11 and
where the difference between the calls in g and h is.

Can anyone explain this?


A template member of a template needs to be specified as such to help
the compiler resolve the name. I don't remember the exact paragraph
of the Standard, but you could search the archives on Google, it has
come up more than once in this newsgroup.

V

Thank you, Victor.
I have never seen this before. Even in the Stroustrup I could not find it.

What I still not understand is why the call in h is valid and the call
in (the template function) g is not valid.

Gabriel
 
V

Victor Bazarov

Gabriel said:
Victor said:
john.constantine said:
Hi
I have this code:

[begin main.cpp]
template<typename ClassType>
struct S
{
template<typename FunctionType> void member() {};



Drop the trailing semicolon: while it is not a severe violation,
it is entirely superfluous and a bad habit.
};

template<typename Type>
void g()
{
S<Type> s;
/*line 11*/ s.member<Type>();



Needs to be
s.template member<Type>();

to be standard C++. MS compiler hasn't got there yet.
}

void h()
{
S<int> s;
s.member<int>();
}

int main(int, char**)
{
g<int>();
h();
return 0;
}
[end main.cpp]

I compile it with g++ (GCC) 3.4.2 (mingw-special) and get this
compiler error:
../main.cpp: In function `void g()':
../main.cpp:11: error: expected primary-expression before '>' token
../main.cpp:11: error: expected primary-expression before ')' token

I do not understand what is wrong with the expression in Line 11 and
where the difference between the calls in g and h is.

Can anyone explain this?



A template member of a template needs to be specified as such to help
the compiler resolve the name. I don't remember the exact paragraph
of the Standard, but you could search the archives on Google, it has
come up more than once in this newsgroup.

V


Thank you, Victor.
I have never seen this before. Even in the Stroustrup I could not find it.

What I still not understand is why the call in h is valid and the call
in (the template function) g is not valid.

'h' is not a template itself. 'g' is a template. The names are resolved
differently inside those scopes. Get a copy of "C++ Templates" by David
Vandevoorde and Nicolai Josuttis. It's an extremely helpful book. It
doesn't just tell you how, it explains why certain things are done the way
they are done.

V
 
A

annamalai.gurusami

Victor said:
Needs to be
s.template member<Type>();

to be standard C++. MS compiler hasn't got there yet.

I haven't yet written (or maintained) template heavy C++ code.
So, this is new for me (obvious, if you had a look at my
previous post, which the OP was kind enough to call nifty.)

My question is if the original code was incorrect, and MS
compiler compiled it successfully, what does it mean? Will
the MS compiler generate wrong object code for correct
(standard compliant) C++ code, in some very rare cases?
(Probably template heavy stuff.)

Any thoughts on that?

Rgds,
anna
 
G

Gabriel

Victor Bazarov wrote:




I haven't yet written (or maintained) template heavy C++ code.
So, this is new for me (obvious, if you had a look at my
previous post, which the OP was kind enough to call nifty.)

My question is if the original code was incorrect, and MS
compiler compiled it successfully, what does it mean? Will
the MS compiler generate wrong object code for correct
(standard compliant) C++ code, in some very rare cases?
(Probably template heavy stuff.)

Any thoughts on that?

Rgds,
anna
AS far as I understand it, the MS compiler performs lookup that it
shouldn't (like the one in the example). This works in usual cases but
there might be complicated cases where the lookup cannot be done, and
this might cause very hard-to-understand errors.

The MS compiler is known to be unable to translate some template heavy code.

Gabriel
 
V

Victor Bazarov

Victor Bazarov wrote:




I haven't yet written (or maintained) template heavy C++ code.
So, this is new for me (obvious, if you had a look at my
previous post, which the OP was kind enough to call nifty.)

My question is if the original code was incorrect, and MS
compiler compiled it successfully, what does it mean?

It's called a "language extension". See more musings below.
> Will
the MS compiler generate wrong object code for correct
(standard compliant) C++ code, in some very rare cases?
(Probably template heavy stuff.)

Any thoughts on that?

Well, we cannot say anything about the correctness of the generated code,
since it's generated from a non-standard extension. However, the VC++ is
known to also accept the standard code , and all we can do about the
situation is to compare the codes generated from either C++ program (with
and without the 'template' keyword) and conclude that the non-standard
source yields the same machine code, for example.

The behaviour of the two resulting programs (from the non-standard source
and the amended one) can also be compared. Whether it has any bearing on
the results of compiling _similar_ programs, I don't know. Maybe. You
should also ask in microsoft.public.vc.language newsgroup, it's full of
very knowledgeable folk, especially when it comes to VC++.

VC++ has got much better over the past couple of years (I consider v7.1
the first really worth using if you want to learn standard C++, v8 is even
better). But it still has some quirks, especially when it comes to being
able to compile non-standard code without even a warning. AFAIK, VC++
team is not going to do much about those "bugs" in their compiler simply
because VC++ successfully compiles the well-formed code. Issuing warnings
or other diagnostics about ill-formed programs is often up to the
implementors. The compiler is free to keep compiling ill-formed code and
generate what it considers to be the best approximation of what the
programmer wanted the code to do. It's non-portable, yes, but it's fully
within the language specifications.

V
 
A

Alipha

Victor said:
john.constantine said:
Hi
I have this code:

[begin main.cpp]
template<typename ClassType>
struct S
{
template<typename FunctionType> void member() {};

Drop the trailing semicolon: while it is not a severe violation,
it is entirely superfluous and a bad habit.
};

template<typename Type>
void g()
{
S<Type> s;
/*line 11*/ s.member<Type>();

Needs to be
s.template member<Type>();

to be standard C++. MS compiler hasn't got there yet.
}

void h()
{
S<int> s;
s.member<int>();
}

int main(int, char**)
{
g<int>();
h();
return 0;
}
[end main.cpp]

I compile it with g++ (GCC) 3.4.2 (mingw-special) and get this compiler
error:
../main.cpp: In function `void g()':
../main.cpp:11: error: expected primary-expression before '>' token
../main.cpp:11: error: expected primary-expression before ')' token

I do not understand what is wrong with the expression in Line 11 and
where the difference between the calls in g and h is.

Can anyone explain this?

A template member of a template needs to be specified as such to help
the compiler resolve the name. I don't remember the exact paragraph
of the Standard, but you could search the archives on Google, it has
come up more than once in this newsgroup.

V

they're called dependent names, and here's a good site which explains
them. http://womble.decadentplace.org.uk/c++/template-faq.html
 
M

Marc Mutz

john.constantine said:
/*line 11*/        s.member<Type>();

s.template member<Type>();

's' depends on the template argument, so you need to tell
the compiler that you mean to call a template member
function, and not
operator<( s.member, Type )...
or something like that.

Marc
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top