Jump to content
  • Advertisement
Sign in to follow this  

_declspec(align(16)) vs alignas(16)

Recommended Posts

Visual Studio supports C++11's alignas since version 2015. Though, in much samples from Microsoft, I still notice the use of "_declspec(align(16))". I guess the reason is for backwards compatibility with Visual Studio 2013. C++17 extends the alignas behavior by also ensuring the right alignment for dynamic memory allocation. Before C++17, one would have to override the operators new, new[], delete, delete[] for the right alignment of dynamically allocated memory. So is there still some non-mentioned benefit in preferring _declspec(align(16)) over alignas(16)? Note that with alignas it is still possible to override the operators new, new[], delete, delete[].


Share this post

Link to post
Share on other sites

Well you mostly point it out yourself: __declspec(align(x)) (note the double underscore, by the way) has largely been superseded by alignas. If you really need to support older versions of C++ where it was not present, it might be of use, though I'd use something like this myself

#if ...
	#define ALIGNAS(x) alignas(x)
  	#define ALGINAS(x) __declspec(align(x))

But it is, of course, not necessary and you can stick with the pre-c++11 version if you want to ensure that backwards compatibility.

Note that GCC/Clang typically require __attribute__((aligned(x)) instead, if you were planning to support either by any chance. 

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!