By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,837 Members | 1,637 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,837 IT Pros & Developers. It's quick & easy.

problem: calling a template member function

P: n/a
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
Aug 25 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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

Aug 25 '05 #2

P: n/a
Frank Chang wrote:
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?
Aug 25 '05 #3

P: n/a
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
Aug 25 '05 #4

P: n/a
Gabriel wrote:

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

Aug 25 '05 #5

P: n/a
john.constantine wrote:
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
Aug 25 '05 #6

P: n/a
an****************@gmail.com wrote:
Gabriel wrote:
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


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

Gabriel
Aug 25 '05 #7

P: n/a
Victor Bazarov wrote:
john.constantine wrote:
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
Aug 25 '05 #8

P: n/a
Gabriel wrote:
Victor Bazarov wrote:
john.constantine wrote:
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
Aug 25 '05 #9

P: n/a
Victor Bazarov wrote:
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

Aug 25 '05 #10

P: n/a
an****************@gmail.com wrote:
Victor Bazarov wrote:

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

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
Aug 25 '05 #11

P: n/a
an****************@gmail.com wrote:
Victor Bazarov wrote:

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?


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
Aug 25 '05 #12

P: n/a

Victor Bazarov wrote:
john.constantine wrote:
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...plate-faq.html

Aug 25 '05 #13

P: n/a
john.constantine wrote:
/*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

Aug 25 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.