Std :: align is not supported by g ++ 4.9

Studying alignment issues, etc., I realized that my implementation of g ++ 4.9 (macports OS X) does not support std::align . If I try to compile (with -std=c++11 ) this sample code from http://www.cplusplus.com/reference/memory/align/

 // align example #include <iostream> #include <memory> int main() { char buffer[] = "------------------------"; void * pt = buffer; std::size_t space = sizeof(buffer) - 1; while ( std::align(alignof(int), sizeof(char), pt, space) ) { char* temp = static_cast<char*>(pt); *temp = '*'; ++temp; space -= sizeof(char); pt = temp; } std::cout << buffer << '\n'; return 0; } 

the compiler gives an error

 error: 'align' is not a member of 'std' 

This seems strange, since g ++ seems to have implemented alignment support with g ++ 4.8, https://gcc.gnu.org/projects/cxx0x.html (N2341)

The code compiles under clang ++ without any problems.

Is this a well-known g ++ issue that I don't know about? The online compilers I tested (ideone and coliru) also reject the code.

+8
c ++ alignment c ++ 11
source share
2 answers

Yes, this is a famous missing feature for gcc:

+12
source share

Alternatively, you can write your own alignment code that matches the behavior of std::align . The following code snippet was written by David Kraus in a post found here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57350

 inline void *align( std::size_t alignment, std::size_t size, void *&ptr, std::size_t &space ) { std::uintptr_t pn = reinterpret_cast< std::uintptr_t >( ptr ); std::uintptr_t aligned = ( pn + alignment - 1 ) & - alignment; std::size_t padding = aligned - pn; if ( space < size + padding ) return nullptr; space -= padding; return ptr = reinterpret_cast< void * >( aligned ); } 
+2
source share

All Articles