C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

mysterious snippet

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
khaeming@web.de
Guest





PostPosted: Tue Oct 18, 2005 10:23 am    Post subject: mysterious snippet Reply with quote



Hello everyone,

I'm new to this group hoping to get a little help.
To start, here is a condensed version of my current problem:

Can I use the the template class C as a template-parameter on the line
marked with (!!!)?

class A
{
public:
A(){;}
~A(){;}
template <class T> T getIt(T i){return i + i;}
};

template <class C>
class B
{
public:
B(){;}
~B(){;}
void nix()
{
A a;
int i = a.getIt<C>(4); //(!!!)
i = i;
}
};

int main()
{
B<int> b;
b.nix();
return 0;
}

The compiler I use is the gcc, version 3.3.5.
On the line marked with (!!!) it says:
"error: parse error before `;' token"
I like these errors that tell you absolutely nothing ;)

So, is this an problem of the language itself? Or is it just a compiler
issue and I am posting in the wrong group?

Many thanks for your help,

Klaus


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Tue Oct 18, 2005 9:33 pm    Post subject: Re: mysterious snippet Reply with quote



[email]khaeming (AT) web (DOT) de[/email] wrote:

Quote:

Can I use the the template class C as a template-parameter on the line
marked with (!!!)?

snip

The compiler I use is the gcc, version 3.3.5.
On the line marked with (!!!) it says:
"error: parse error before `;' token"
I like these errors that tell you absolutely nothing Wink


The code is correct and compiles on both VC7.1 and gcc 4.x. Looks like a
compiler bug. However, there's an easy fix, just change the offending
line to:

int i = a.template getIt<C>(4);
^^^^^^^^

the extra "template" keyword tells the compiler that getIt is a name of
a member template and not a regular member. As I said, the original code
is correct, so the keyword should not be needed in this context, because
"a" is a non-dependent name and the compiler can determine that getIt is
the name of a template. However, it seems that gcc 3.3.5 is not smart
enough and the extra help makes it happy. BTW, the .template syntax
*would* be needed if "a" were a dependent name because in that case the
compiler could not determine if the subsequent '<' would have to be
interpreted as a left angular bracket or as a less-than sign.

However, this raises an interesting question: is the .template syntax
allowed in this case? According to Vandevoorde and Josuttis' "C++
Templates", except for the case in which the .template syntax is
required, it's not allowed, although the standard isn't explicit about
that. However, both gcc 4.x and VC7.1 happily ignore the redundant
syntax. I like gcc and VC relaxedness here, it makes sense, but I feel
the standard should clarify the issue.

HTH,

Ganesh


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Bo Persson
Guest





PostPosted: Tue Oct 18, 2005 9:35 pm    Post subject: Re: mysterious snippet Reply with quote




<khaeming (AT) web (DOT) de> skrev i meddelandet
news:1129623826.546757.285530 (AT) g43g2000cwa (DOT) googlegroups.com...

Quote:
Hello everyone,

I'm new to this group hoping to get a little help.
To start, here is a condensed version of my current problem:

Can I use the the template class C as a template-parameter on the
line
marked with (!!!)?

class A
{
public:
A(){;}
~A(){;}
template <class T> T getIt(T i){return i + i;}
};

template <class C
class B
{
public:
B(){;}
~B(){;}
void nix()
{
A a;
int i = a.getIt i = i;
}
};

int main()
{
B<int> b;
b.nix();
return 0;
}

The compiler I use is the gcc, version 3.3.5.
On the line marked with (!!!) it says:
"error: parse error before `;' token"
I like these errors that tell you absolutely nothing ;)

So, is this an problem of the language itself? Or is it just a
compiler
issue and I am posting in the wrong group?


It seems to compile fine with other compilers, like MS and Comeau.
That makes 3.3.5 a suspect.


Bo Persson



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Victor Bazarov
Guest





PostPosted: Tue Oct 18, 2005 10:43 pm    Post subject: Re: mysterious snippet Reply with quote

[email]khaeming (AT) web (DOT) de[/email] wrote:
Quote:
I'm new to this group hoping to get a little help.
To start, here is a condensed version of my current problem:

Can I use the the template class C as a template-parameter on the line
marked with (!!!)?

class A
{
public:
A(){;}
~A(){;}
template <class T> T getIt(T i){return i + i;}
};

template <class C
class B
{
public:
B(){;}
~B(){;}
void nix()
{
A a;
int i = a.getIt

You probably need
int i = a. template getIt<C>(4);

Quote:
i = i;
}
};

[...]


V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ganny
Guest





PostPosted: Wed Oct 19, 2005 1:57 am    Post subject: Re: mysterious snippet Reply with quote

Quote:
int i = a.getIt<C>(4); //(!!!)
Change it to:

int i = a.template getIt<C>(4);

I've gcc 4.0 and the code works fine without explicit template
keyword....

Thanks!
-Ganesh


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Sumit Rajan
Guest





PostPosted: Wed Oct 19, 2005 2:00 am    Post subject: Re: mysterious snippet Reply with quote


<khaeming (AT) web (DOT) de> wrote


Quote:
class A
{
public:
A(){;}
~A(){;}
template <class T> T getIt(T i){return i + i;}
};

template <class C
class B
{
public:
B(){;}
~B(){;}
void nix()
{
A a;
int i = a.getIt i = i;
}
};

int main()
{
B<int> b;
b.nix();
return 0;
}

The compiler I use is the gcc, version 3.3.5.
On the line marked with (!!!) it says:
"error: parse error before `;' token"
I like these errors that tell you absolutely nothing Wink

The code looks okay to me. Besides, it seems to compile on Comeau C++, g++
3.4.2 and VC++ 7.1.

Regards,
Sumit.
--
Sumit Rajan <sumitr (AT) msdc (DOT) hcltech.com>



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Edson Manoel
Guest





PostPosted: Wed Oct 19, 2005 2:03 am    Post subject: Re: mysterious snippet Reply with quote

Msvc 7.1 compiled it ok. It can be a compiler issue, try this:

template <class C>
class B
{
public:
typedef typename C CType;
B(){;}
~B(){;}
void nix()
{
A a;
int i = a.getIt<CType>(4); //(!!!)
i = i;
}

};

Or this:

int i = a.getIt(4); //(!!!)


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Allan W
Guest





PostPosted: Wed Oct 19, 2005 8:34 am    Post subject: Re: mysterious snippet Reply with quote

[email]khaeming (AT) web (DOT) de[/email] wrote:
Quote:
To start, here is a condensed version of my current problem:
[SNIP]
Can I use the the template class C as a template-parameter on the line
marked with (!!!)?

Compiles without error on Microsoft Visual Studio .Net 2003.
Which doesn't prove it's legal... but I think it is.

Quote:
The compiler I use is the gcc, version 3.3.5.
On the line marked with (!!!) it says:
"error: parse error before `;' token"
I like these errors that tell you absolutely nothing Wink

Hmm, I don't use GCC but it has a better reputation than that.
Any chance you're compiling it as a C program, instead of a C++
program? Just a wild guess...

Quote:
So, is this an problem of the language itself? Or is it just a compiler
issue and I am posting in the wrong group?

Even if it's "just" a compiler issue, you can post questions here
about how to get it to compile portably.

Good luck.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Abhishek Pamecha
Guest





PostPosted: Wed Oct 19, 2005 8:40 am    Post subject: Re: mysterious snippet Reply with quote

May be I dont understand your requirements but ..
Why would you need a template here? C seems obviously an integer (or it
cousins like long, unsigned int, long long, float etc /. Smile)

the conversion here should be handled automatically by the compiler if
you are not causing information loss. If you are causing information
loss, either this will only hide the problem or the compiler will warn
you while compiling.

In either case, I would not recommend this templatized approach unless
you have really diverse plans.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.