473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

problem: calling a template member function

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
13 10636
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
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
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
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
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
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
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
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
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: tyousaf | last post by:
Hi i am new to mysql and mysql++, i have installed mysql server, it is running fine. i also installed "mysql++-1.7.9gcc3.2-2.i386.rpm" (i have gcc 3.3) , first of all as the readme file says to do...
3
by: ogtindeed | last post by:
Hi all, I need some help. I am trying to instantiate a Template and call a member function, but have not been able - Now this is what I am trying to do: foo.h =========
4
by: Martin MacRobert | last post by:
Hi Gang, The following code does not compile, but I can't figure out why. The compiler complains that the CuriouslyDerivedType (CRDerived) does not publish the "value_type", yet in fact the class...
2
by: Robbie Hatley | last post by:
I've got a function that I use a lot when making utility programs that need to do the same thing to every directory in a tree. Its prototype is: unsigned long int CursDirs (void Func(void)); ...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
25
by: Nafai | last post by:
Hello. I'll try to explain my problem with an example: I have the following classes: class A { public: string name; .... };
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
3
by: StephQ | last post by:
In utility.hpp I have: namespace utility { template <class T, double (T::*F)(double) const> class Display { private: static double resolution;
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.