473,569 Members | 2,880 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Possible g++ bug in template parsing

The attached code compiles and works properly when I comment out the
declaration, definition, and invocations of the method 'eck'. With
"eck" in there, g++ fails with

ttest.cpp:23: template-id `eck<>' for `void blah<int>::eck( int)'
does not match any template declaration
ttest.cpp:23: syntax error before `{' token
ttest.cpp:25: syntax error before `<<' token

Note that 'gak' (a template function in a template class) works, as
does
'ugh" (a normal function in a specialized template class); it's only
'eck', a template function in a specialized template class, that
fails.

The compiler was g++ 3.2.2 20030222 on Red Hat 9 i386.

g++ 2.95.3 on Solaris SPARC 8 failed, with a different error.

It did compile and run on Solaris 8 using Sun's C++ compiler. This
doesn't help me much, since the code must run on Linux i386, but
suggests that the code is probably valid.

Unfortunately, I don't have access to a system with a latest-release
gcc to test.

Do I have a gcc bug, or is this code actually invalid?

If it is a bug, does anyone know of a workaround I could use for the
time being? If it's bad code, then what am I doing wrong? Is Sun's
CC in error compiling this?

Thanks for the help.
Rennie deGraaf

-------------------------------------

#include <iostream>
#include <typeinfo>
using namespace std;

template <class TYPE>
class blah
{
TYPE feh;
public:
void blech(int x);
template <class T> void gak(int x);endl; }
template <class T> void eck(int x);
void ugh(int x);
};

template <class TYPE> template <class T> void blah<TYPE>::gak (int x)
{
T a;
cout << typeid(a).name( ) << endl;
}

template <> template <class T> void blah<int>::eck( int x)
{
T a;
cout << typeid(a).name( ) << typeid(feh).nam e() << endl;
}

template <class TYPE> void blah<TYPE>::ble ch(int x)
{
cout << typeid(feh).nam e() << endl;
}

template <> void blah<int>::ugh( int x)
{
cout << typeid(feh).nam e() << endl;
}
/*template <> template <class T> void blah<char>::eck (int x)
{
T a;
cout << "calling blah<char>::eck (int) " << typeid(feh).nam e() <<
"\n";
}*/

template <class T>
void foobar(int x)
{
T a;
cout << typeid(a).name( ) << endl;
}

int main()
{
cout << "call foobar with int: ";
foobar<int>(1);
cout << "call foobar with char: ";
foobar<char>(1) ;
cout << "call foobar with short: ";
foobar<float>(1 );
cout << "call foobar with int*: ";
foobar<int*>(1) ;

blah<int> a;
blah<char> b;
blah<float> c;
blah<int*> d;

cout << "call blah<int>.blech (): ";
a.blech(1);
cout << "call blah<char>.blec h(): ";
b.blech(1);
cout << "call blah<float>.ble ch(): ";
c.blech(1);
cout << "call blah<int*>.blec h(): ";
d.blech(1);

cout << "call blah<int>.gak<f loat>(): ";
a.gak<float>(1) ;

cout << "call blah<int>.ugh() : ";
a.ugh(1);
// cout << "call blah<char>.ugh( ): ";
// b.ugh(1); // this isn't defined, and
shouldn't work

cout << "call blah<int>.eck<f loat>(): ";
a.eck<float>(1) ;
// cout << "call blah<char>.eck< float>(): ";
// b.eck<float>(1) ; // this isn't defined, and
shouldn't work
return 0;
}
Jul 19 '05 #1
3 2312
"Rennie deGraaf" <re****@my-dejanews.com> wrote in message
news:7c******** *************** ***@posting.goo gle.com...
The attached code compiles and works properly when I comment
out the declaration, definition, and invocations of the method 'eck'.
With "eck" in there, g++ fails with

ttest.cpp:23: template-id `eck<>' for `void blah<int>::eck( int)'
does not match any template declaration
ttest.cpp:23: syntax error before `{' token
ttest.cpp:25: syntax error before `<<' token
I think this is correct.
Note that 'gak' (a template function in a template class) works,
as does 'ugh" (a normal function in a specialized template class);
it's only 'eck', a template function in a specialized template class,
that fails. [...]
Do I have a gcc bug, or is this code actually invalid?
Bad code.
If it is a bug, does anyone know of a workaround I could use for
the time being? If it's bad code, then what am I doing wrong?
Is Sun's CC in error compiling this?
Looks like it to me.
[...]
template <class TYPE>
class blah
{
TYPE feh;
public:
void blech(int x);
template <class T> void gak(int x);endl; }
This obviously isn't a cut-n-paste, or this line shouldn't compile.
template <class T> void eck(int x);
void ugh(int x);
};

template <> template <class T> void blah<int>::eck( int x)
{
T a;
cout << typeid(a).name( ) << typeid(feh).nam e() << endl;
}
[...]
template <> void blah<int>::ugh( int x)
{
cout << typeid(feh).nam e() << endl;
}
[...]


At no point do you define the specialization:

template <>
class blah<int>
{
...
};

Therefore, defining member functions of this specialization
is illegal. The solution is to define the class specialization.

Dave

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
Jul 19 '05 #2
"David B. Held" <dh***@codelogi cconsulting.com > wrote in message news:<bn******* ***@news.astoun d.net>...
"Rennie deGraaf" <re****@my-dejanews.com> wrote in message
news:7c******** *************** ***@posting.goo gle.com...
The attached code compiles and works properly when I comment
out the declaration, definition, and invocations of the method 'eck'.
With "eck" in there, g++ fails with

ttest.cpp:23: template-id `eck<>' for `void blah<int>::eck( int)'
does not match any template declaration
ttest.cpp:23: syntax error before `{' token
ttest.cpp:25: syntax error before `<<' token
I think this is correct.
Note that 'gak' (a template function in a template class) works,
as does 'ugh" (a normal function in a specialized template class);
it's only 'eck', a template function in a specialized template class,
that fails.

[...]
Do I have a gcc bug, or is this code actually invalid?


Bad code.
If it is a bug, does anyone know of a workaround I could use for
the time being? If it's bad code, then what am I doing wrong?
Is Sun's CC in error compiling this?


Looks like it to me.
[...]
template <class TYPE>
class blah
{
TYPE feh;
public:
void blech(int x);
template <class T> void gak(int x);endl; }


This obviously isn't a cut-n-paste, or this line shouldn't compile.


Sorry about that; I deleted a multi-line comment when I posted it, and
evidently missed part. The rest IS a cut-and-paste.
template <class T> void eck(int x);
void ugh(int x);
};

template <> template <class T> void blah<int>::eck( int x)
{
T a;
cout << typeid(a).name( ) << typeid(feh).nam e() << endl;
}
[...]
template <> void blah<int>::ugh( int x)
{
cout << typeid(feh).nam e() << endl;
}
[...]
At no point do you define the specialization:

template <>
class blah<int>
{
...
};

Therefore, defining member functions of this specialization
is illegal. The solution is to define the class specialization.


Then how come this is accepted, and works properly?

template <> void blah<int>::ugh( int x)
{
cout << typeid(feh).nam e() << endl;
}
Dave

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003

Jul 19 '05 #3
"Rennie deGraaf" <re****@my-dejanews.com> wrote in message
news:7c******** *************** ***@posting.goo gle.com...
[...]
Then how come this is accepted, and works properly?

template <> void blah<int>::ugh( int x)
{
cout << typeid(feh).nam e() << endl;
}


Because the compiler is broken? Usually, when something
unexpectedly works, something else (that should) doesn't.
Try this, for instance, define the specialization *after* this
definition, and see what the compiler says.

Dave

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
1776
by: comp.lang.php | last post by:
I've heard numerous and varied commentaries here and on other fora regarding PHP and the concept of threads. Coming from a Java background I understand how threads benefit to prevent collisions, all the while carefully written to avoid "race conditions" while a web application is being utilized in a multi-user environment. However, are...
2
1734
by: bettyann | last post by:
i am using templates via HTML_Template_IT to create a table from the results of a mysql query. i wanted to change the background image of alternating rows in the table. i thought i could do this by defining two different blocks in the template file and setting the current block based on ( $thisRowNum % 2 ). when the final table is...
2
2001
by: Richard Cromer | last post by:
Greetings, I am trying to create a template class called Matrix that operates on a template class called complex. Now of course complex is a template class and I want it to be the argument for Matrix. So I have something like: template <class T> class complex {/*..*/}
2
1188
by: JT | last post by:
I have a compiler error when using a non-dependent type declared in a template, if I use the type in function definitions. I think it is a parsing issues related to confusion with a typename. Below I have a simple example. Is there another solution I should be using instead of declaring the type outside of the template? Thanks, JT
2
1489
by: max_sang | last post by:
Hello I have a nasty problem... take a look at this code: struct Parser { Parser(const string& s) { ... tokenizes s into pieces... } template <class T> to(size_t idx); private: vector<string> tokens_; // holds the pieces of s };
6
2826
by: Stuart McGraw | last post by:
I am looking for a VBA "format" or "template" function, that is, a function that takes a format string and a varying number of arguments, and substitutes the argument values into the format string as specified in the format string. For example fmt("this is $1 format string. arg2 is $2", "short", 3) would return the string "this is short...
2
1269
by: booker | last post by:
the following codes can pass the compilation in VC6 but not in VC7.1.. is it a bug? #include <list> template<class type> class myclass { typedef myclass<type> mytype;
2
2190
by: RitualDave | last post by:
I get a C2811 error when I compile the following (with the /clr switch): template <class T> ref class Base {}; template <template <class> class TBase> ref class Derived : TBase<int> {};
2
313
by: a | last post by:
I'm having problem compiling this simple template program, my g++ complains it doesn't know what size and ia are on ArrayRC. TIA, John #include <iostream.h>
0
7619
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
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...
0
3662
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...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
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
0
950
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...

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.