On Tue, 7 Apr 2015, Jonathan Wakely wrote:
> On 07/04/15 06:51 -0400, Hans-Peter Nilsson wrote:
> > On Tue, 7 Apr 2015, Jonathan Wakely wrote:
> > > On 05/04/15 21:07 -0400, Hans-Peter Nilsson wrote:
> > > > We did specify that with the alignas.  Is the alignof always
> > > > exactly the same as an alignas, if one is specified?  (And will
> > > > that not change in a future amendment, standard and/or
> > > > implementation?)  Either way, is there a test-case to guard all
> > > > this?
> > >
> > > The language guarantees that's what alignas() does, if the argument is
> > > a valid alignment (which it must be if we derive it from some other
> > > type's alignment).
> >
> > I'm more worried about alignof reporting a higher value for a
> > specific object than alignas to be wrong.
>
> That shouldn't be possible because the C++ standard says it's an error
> to use alignas with a less strict alignment than would be used if it
> was omitted, i.e. an error to use alignas with a value less than the
> result alignof would give.  However, G++ doesn't reject it (PR65685).
>
> It still won't be possible here, because the alignas value we use is
> not less than alignof(_Tp).

That's not what I meant.  My worry is there being a case where
alignof yields a *higher* value than the one that the alignas
specified.

> > Your question quoted just below seems to indicate a similar
> > worry.
>
> I was thinking about cases like this:
>
>  struct __attribute__((packed)) Bad {
>    char c;
>    std::atomic<long long> a;
>  };
>
> But G++ ignores the packed attribute here, which is good (Clang
> doesn't seem to ignore it, and mis-aligns the atomic).

I was more thinking of something like:

#include <atomic>
#include <iostream>
using std::cout;
using std::endl;

struct SoSo {
 double d;
 int x alignas(sizeof(int));
};

SoSo s __attribute__((__aligned__(16)));

int main(void)
{
  cout << "alignof(s): " << alignof(s) << endl;
  cout << "alignof(s.d): " << alignof(s.d) << endl;
  cout << "alignof(s.x): " << alignof(s.x) << endl;
}

in which I fear s.x would get an alignof the same as the s.d or
s, now or after a while, i.e. higher than specified.

(I get for cris-elf at revision 221891:
alignof(s): 16
alignof(s.d): 1
alignof(s.x): 4
which is kind-of-expected except I thought s.d would get the s
alignment so that just leaves it open whether that could
possibly change.)

brgds, H-P

Reply via email to