Board index » cppbuilder » templates and pragma pack ?

templates and pragma pack ?


2005-11-19 09:01:07 PM
cppbuilder111
pragma pack does not works with templates
#pragma pack(push,1)
template<class T>class AClass {
T f_val1;
T f_val2;
public:
/*....*/
};
#pragma pack(pop)
typedef AClass<int>int_c; // <------ sizeof give 8, ok
typedef AClass<short>short_c; // <------ sizeof give 8 again :-(
Ok I knew it. Now can I do it and how ?
Thank you
s.py
 
 

Re:templates and pragma pack ?

I forgot to say that:
obviously the template is declared with no virtual method, avoiding the
compiler to add the vmt pointer
so no error generated with the following:
typedef AClass<int>int_c;
#if sizeof(int_c) != 2*sizeof(int)
# error sizeof(int_c) <---- this error is never generated
#endif
but the following do:
typedef AClass<short>short_c;
#if sizeof(short_c) != 2*sizeof(short)
# error sizeof(short_c) <---- this error is always generated
#endif
I tryied too to pack the typedef, there is no change in the size:
#pragma pack(push,1)
typedef AClass<short>short_c;
#pragma pack(pop)
"py" < XXXX@XXXXX.COM >a écrit dans le message de news:
XXXX@XXXXX.COM ...
Quote
pragma pack does not works with templates

#pragma pack(push,1)
template<class T>class AClass {
T f_val1;
T f_val2;
public:
/*....*/
};
#pragma pack(pop)
typedef AClass<int>int_c; // <------ sizeof give 8, ok
typedef AClass<short>short_c; // <------ sizeof give 8 again :-(

Ok I knew it. Now can I do it and how ?
Thank you

s.py



---
avast! Antivirus: message Entrant propre.
Base de donnÚes des virus (VPS): 0546-3, 16/11/2005
Test du: 19/11/2005 14:30:38
avast! - copyright (c) 1988-2004 ALWIL Software.
www.avast.com



 

Re:templates and pragma pack ?

py wrote:
Quote
pragma pack does not works with templates

#pragma pack(push,1)
template<class T>class AClass {
T f_val1;
T f_val2;
public:
/*....*/
};
#pragma pack(pop)
typedef AClass<int>int_c; // <------ sizeof give 8, ok
typedef AClass<short>short_c; // <------ sizeof give 8 again :-(

Ok I knew it. Now can I do it and how ?
I think your are looking at the wrong thing.
Compile and run this from a command prompt:
#include <stdio.h>
#pragma pack(push,1)
template<class T>class AClass {
T f_val1;
T f_val2;
public:
/*....*/
};
#pragma pack(pop)
typedef AClass<int>int_c;
typedef AClass<short>short_c;
int main(void)
{
int_c i[10];
short_c s[20];
printf( "\rSize of i = %i, Size of s = %i\r\n",
sizeof(i), sizeof(s) );
return 0;
}
C:\test>pack
Size of i = 80, Size of s = 80
C:\test>
 

{smallsort}

Re:templates and pragma pack ?

Thank you, I checked and you are right on that (and I should have tryed that
!) But I m right too. With your help I ve found the core of the problem, and
by the hell I'll find a workaround.
My template is in fact more complicated and I use embedded types.
In the following code the struct AType is not byte aligned, and the class
size is 8 both for int and short.
#pragma pack(push,1)
template<class T>class AClass {
typedef struct {
T f_val1;
T f_val2;
} AType;
AType f_val;
public:
/*....*/
};
#pragma pack(pop)
Note that I really need subtypes bc the actual implemented struct has unions
inside. The class is a really core and specialized one for the application
and I really need to optimize it, size and speed. In this specific problem
Im not concerned in portability nor 'good writing'
s.py
"Bob Gonder" < XXXX@XXXXX.COM >a écrit dans le message de news:
XXXX@XXXXX.COM ...
Quote

I think your are looking at the wrong thing.
Compile and run this from a command prompt:

#include <stdio.h>

#pragma pack(push,1)
template<class T>class AClass {
T f_val1;
T f_val2;
public:
/*....*/
};
#pragma pack(pop)
typedef AClass<int>int_c;
typedef AClass<short>short_c;

int main(void)
{
int_c i[10];
short_c s[20];

printf( "\rSize of i = %i, Size of s = %i\r\n",
sizeof(i), sizeof(s) );
return 0;
}

C:\test>pack
Size of i = 80, Size of s = 80

C:\test>