473,804 Members | 2,296 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem: calling a template member function

Hi
I have this code:

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

template<typena me 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
13 10689
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.publi c.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.constantin e wrote:
Hi
I have this code:

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


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

template<typena me 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.constantin e wrote:
/*line 11*/********s.membe r<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
2398
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 automake, if i do that in the proper director it says "automake: 'configure.ac' or 'configure.in' is required" i dont know where to bring those files. secondly if i only compile simple1.cc using gcc proving all the link flags to libraries and...
3
1383
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
1496
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 CRDerived does. Is there any way of making the code work? I would like functions to accept a CRBase template, and the CRBase must be able to deduce the "value_type" of the function that it is "curiously calling". Thanks.
2
1996
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)); This just applies the fuction Func to every subdirectory of the current directory. It works fine when I pass it pointers to regular void-void functions.
7
2138
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
1727
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
9681
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
5791
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
391
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
9714
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.