Re: A portable way of pointer alignment
* Chris M. Thomasson:
"Alf P. Steinbach" <alfps@start.no> wrote in message
news:hm74pd$rk7$1@news.eternal-september.org...
* Chris M. Thomasson:
"Chris M. Thomasson" <no@spam.invalid> wrote in message
news:HlEhn.17$NH1.0@newsfe14.iad...
"Alf P. Steinbach" <alfps@start.no> wrote in message
news:hm72il$5eb$1@news.eternal-september.org...
[...]
Reposting that:
<code>
typedef ptrdiff_t Size;
template< typename T >
inline Size alignmentOf()
{
struct X
{
char bah;
T t;
};
return offsetof( X, t );
}
I don't think `offsetof()' works with POD.
YIKES! I meant to say:
I don't think `offsetof()' works with non-POD types.
;^o
Formally it doesn't, in practice it may and probably will, depending
on the compiler.
After all there's not much magic underneath.
Of course that also applies to the macro that you posted. :-)
Well, I only use that macro in C where everything is a POD. For C++ I
use something like:
http://pastebin.org/96326
Which seems to be working fine for everything I throw at it so far,
including reference types.
Yah, it's better :-), because it's simpler & more well-defined.
I wonder whether the constructor you put in there is really necessary though.
At first glance apparently it supports non-default-constructible type T by
assuming that T is copy constructible. But thinking about it, what it does seems
to be to limit T to copy constructible types. So how about, if the compiler
really insists on some constructor, just /declaring/ a default constructor and
not implementing, like
struct object
{
T m_object;
object();
};
After all, you'll never create any actual instance of this class?
I also believe that it has a problem with reference types.
Hm?
For instance, I cannot get the following to compile and run correctly:
___________________________________________________________
#include <cstddef>
#include <iostream>
template< typename T >
inline std::size_t alignmentOf()
{
struct X
{
char bah;
T t;
};
return offsetof( X, t );
}
struct object
{
char m_1[2];
double m_d[6];
long m_2[2];
short m_3[14];
};
int
main()
{
std::cout << "alignmentOf<object&> == "
<< alignmentOf<object&>()
<< std::endl;
return 0;
}
___________________________________________________________
On Comeau I get:
___________________________________________________________
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 8: warning: class "X" defines no constructor to
initialize the
following:
reference member "X::t"
struct X
^
detected during instantiation of
"size_t alignmentOf<T>() [with T=object &]" at line 32
"ComeauTest.c", line 14: error: expression must have a constant value
return offsetof( X, t );
^
detected during instantiation of
"size_t alignmentOf<T>() [with T=object &]" at line 32
1 error detected in the compilation of "ComeauTest.c".
___________________________________________________________
For MingW 3.4.5 I get the following warnings:
___________________________________________________________
In function `size_t alignmentOf() [with T = object&]':
instantiated from here
[Warning] invalid access to non-static data member `alignmentOf() [with
T = object&]::X::t' of NULL object
[Warning] (perhaps the `offsetof' macro was used incorrectly)
___________________________________________________________
Then when I run the program, it crashes. What am I doing wrong here Alf?
Using a reference?
<g>
My hm was more like, why would you want to deal with that case?
Cheers,
- Alf