1

I'm writing a few classes and structs that could benefit from 16-byte alignment. Instead of using compiler-specific hacks, I'd rather use the new C++0x alignas functionality for future portability. However, using it outside of appropriate #ifdef or #ifndef guards will obviously cause errors on compilers without alignas support.

I did a quick search for similar questions, and the closest match had an answer recommending Boost.Config. Unfortunately, Boost.Config doesn't seem to include any functionality for querying alignas support. Are there any other #ifdefs or #ifndefs I can use to figure out whether the compiler supports it?

Thanks!

Community
  • 1
  • 1
Mike S
  • 531
  • 4
  • 14
  • This is what Autoconf is for surely? – Steve-o Aug 24 '11 at 04:32
  • Is there a way to specifically check for alignas support with autoconf? Even if there is though, I don't think autoconf is an option for me. Even if I didn't dread the thought of learning configuration scripting, it's only applicable to the GNU toolchain anyway, right? In the general sense, I wouldn't want to restrict my code to the GNU build system. In my specific case, I'm writing a C++ plugin for Avisynth, which needs to be compiled by MSVC anyway (although some have had success with ICC). – Mike S Aug 24 '11 at 04:48
  • BTW, I understand that I could just use __declspec(align(16)) for this particular project (now that I've specified MSVC ;)). Similarly, I could just use __attribute__ ((aligned (16))) if I were specifically targeting GCC (which I'm not in this case). In other words, being unable to test for alignas(16) isn't exactly a showstopper. I guess I'm just wanting to form a habit of using the new standard functionality where possible. – Mike S Aug 24 '11 at 05:11

1 Answers1

2

There are no direct feature-support macros for the various C++0x (C++11) facilities. There only two ways I can think of to determine their presence.

  1. Keep a list of which compilers support them, and use #ifdef directives based on the compiler-supplied version macros such as __GNUC__ and _MSC_VER, or
  2. Use a feature-detection script such as autoconf to detect compiler support prior to building, and construct a header file with your own macros indicating support for the feature, or lack thereof.

Using Boost.Config is actually an example of both: Boost has a set of feature-detection scripts which are run during Boost development, and then the results hard-coded in the Boost.Config headers based on the compiler version macros.

Anthony Williams
  • 66,628
  • 14
  • 133
  • 155
  • Thank you. Since there aren't any standard macros and Boost.Config devs haven't gotten around to adding a check for that particular feature, I suppose it just comes down to autotools or manually tracking compiler versions. That's a shame, but those are the breaks. :) – Mike S Aug 25 '11 at 01:53