Travis Vitek wrote:
Martin Sebor wrote:

Travis Vitek wrote:
I could implement the specializations of the helper for all possible
values up to the upper limit and then let the compiler puke when it
can't honor the alignment that was requested. It is not
'difficult' to
do this, I just don't see it as useful because I can't test
them until I
have at least one compier that supports the ability to do
non-power-of-two alignments.
That was another question I was going to ask although I think I know
the answer already: the specialization is only necessary for "crappy
compilers" ;-) that require N to be a literal in
__attribute__((aligned(N))), right? If so, I suggest providing the
specializations only for these, shall we say, "limited" compilers
and defining the primary template using the non-type template
parameter N for the rest.

Ummm...

The following code is very similar to that typical
implementation, but it does not compile because value passed to the
aligned attribute has to be a literal, not a constant expression.
That's what you for using a crappy compiler ;-) It compiles
with gcc. See
  http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19163
Uh, yeah, it compiles...

but watch out for:
  http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36625

but it is utterly useless because of this bug. You can't use the nested aligned type. Talk about crappy compilers.
It isn't completely useless because the attribute can successfully
be applied to data members with the same result:

    template <int N>
    struct A {
        struct S {
            short f[3]; __attribute__ ((aligned (N)));
        };
    };

Here is a testcase...

  $ cat z.cpp && g++ --version && g++ -c z.cpp

  template <int N, int A>
struct __rw_aligned_storage { typedef struct {
          char _C_align [N] __attribute__ ((aligned (A)));
      } type;
  };

  __rw_aligned_storage<10, 4>::type aligned_t;

  g++ (GCC) 4.3.1
  Copyright (C) 2008 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There
is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

  z.cpp: In instantiation of '__rw_aligned_storage<10, 4>::type':
  z.cpp:9:   instantiated from here
  z.cpp:5: error: requested alignment is not a constant

If I can't access the member type 'type', then the feature is useless.
The nested type is the only thing that is aligned. If you are sure that
this will work with gcc-4.3, please show me where I've gone wrong.

Isn't it obvious? You're missing a set of parentheses around A ;-)
This works for me:

  template <int N, int A>
  struct __rw_aligned_storage {
      typedef struct {
          char _C_align [N] __attribute__ ((aligned ((A))));
      } type;
  };

  __rw_aligned_storage<10, 4>::type aligned_t;


Travis

We should open an enhancement request with Microsoft.

I'll do that.

Martin



Reply via email to