templates, htonl (volatile) problem
Andrej Filipcic
Andrej.Filipcic@ijs.si
Wed Jul 7 02:22:00 GMT 1999
Hi,
There is a strange behaviour with htonl function , templates and egcs-1.1.2 on
glibc-2.1.1 PII system (RH6.0).
The following program
-----------------
#include <cstdio>
#include <netinet/in.h>
template <class T>
class A
{
public:
A() {};
~A() {};
unsigned int testf() {
return htonl( 0x01020304 );
}
};
int main() {
A<int> a;
printf("%d\n",a.testf());
}
------------------
compiles with no optimization. If g++ -O is invoked, the following message
appears:
0.cc: In method `unsigned int A<int>::testf<int>()':
0.cc:17: instantiated from here
0.cc:11: `volatile' undeclared (first use this function)
0.cc:11: (Each undeclared identifier is reported only once
0.cc:11: for each function it appears in.)
0.cc:11: warning: qualifier ignored on asm
When using -O, htonl is substituted with the following macro (g++ -E -O)
unsigned int testf() {
return (__extension__ ({ register unsigned int __v;
if (__builtin_constant_p ( 0x01020304 ))
__v = (((( 0x01020304 ) & 0xff000000) >> 24) |
((( 0x01020304 ) & 0x00ff0000) >> 8) | ((( 0x01020304 )
& 0x0000ff00) << 8) | ((( 0x01020304 ) & 0x000000ff) << 24)) ;
else
__asm__ __volatile__ ("rorw $8, %w0;" "rorl $16, %0;" "rorw $8, %w0"
: "=r" (__v) : "0" ((unsigned int) ( 0x01020304 ))
: "cc"); __v; })) ;
}
The same code with the same macro compiles without problems,
if templates are skipped. The same happens with 19990615 snapshot.
Thanks for your time
Andrej
_____________________________________________________________
Andrej Filipcic, E-mail: Andrej.Filipcic@ijs.si
Department of Experimental High Energy Physics - F9
Jozef Stefan Institute, Jamova 39, P.o.Box 3000
SI-1001 Ljubljana, Slovenia
Tel.: +386-61-177-3674 Fax: +386-61-125-7074
-------------------------------------------------------------
More information about the Gcc-bugs
mailing list