cannot bind packed field error
Christoph Bartoschek
gcc@pontohonk.de
Tue May 2 19:13:00 GMT 2006
Am Dienstag, 2. Mai 2006 20:40 schrieb Andrew Haley:
> Christoph Bartoschek writes:
> > Am Dienstag, 2. Mai 2006 17:28 schrieb John Love-Jensen:
> > > > Is this an error in the compiler or is there a mistake in the code?
> > >
> > > I think (but I'm not sure) that it is a mistake in the code.
> > >
> > > In that the alignment and packing constraints of a.b.c are different
> > > from a plain old int& and int const&.
> >
> > But why does the call to func(int const &) succeed, if I remove the
> > other func function?
>
> Because the compiler doesn't know what you're trying to do. Let's say
> that you intended to modify the packed field. Clearly the compiler
> can't do that, as a packed int isn't compatible with an int. So what
> would you rather have? A compile-time error, or the compiler silently
> call func(int const &) instead?
You are right that a compile-time error is better in this case, but why don't
I get a comile-timer error, if I use pointers instead of references? They
should be nearly the same:
#include <iostream>
struct A {
union {
int c;
} b;
} __attribute__ ((packed));
void func(int const *) {}
void func(int * p) {
std::cout << *p << std::endl;
*p = 12;
}
int main()
{
A a;
a.b.c = 2;
func(&a.b.c);
std::cout << a.b.c << std::endl;
}
Christoph
More information about the Gcc-help
mailing list