Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Fixes required for a conforming Standard Library

There is one big issue that will make custom allocators fragile to work with: incomplete C++11 library support. Since C++11, containers are required to go through std::allocator_traits<Allocator> to access construct() and destroy() allocator member functions, as well as to access nested typedefs such as pointer and reference. Furthermore, all containers have a constructor with a single allocator argument. Not every Standard Library implements this for every container.

With the Coliru online compiler, I can get Hinnant's stack allocator working with libstdc++ for a std::vector with only 2 minor modifications running

g++ -std=c++11 -O1 -pedantic -Wall -Wconversion -Wsign-conversion -Wsign-promo

The first is to put parenthesis around the n + (alignment - 1) operand in the align_up()

std::size_t 
 align_up(std::size_t n) noexcept
 {return (n + (alignment-1)) & ~(alignment-1);}
 ^ ^

The second is to remove the exception specification from the overloaded operator new

void* operator new(std::size_t s) // throw(std::bad_alloc)

Live Example exactly reproducing Hinnant's first test case.

Note that these 2 warnings are rather innocent and do not affect correctness of the program.

Fixes required for a non-conforming Standard Library

Since libstdc++ works fine with all containers if a std::allocator is provided (even when the container implementation directly accesses the allocator, rather than through std::allocator_traits) the safest bet is to provide all the nested typedefs and member functions of std::allocator_traits in your own allocator as well. This includes the various pointer and reference types, as well as the rebind templates. You appear to have done so, and this should resolve at least of all those issues. I still would leave the original naming of Hinnant's version in tact, though.

That leaves the issues of constructors taking a single allocator argument. At least for libstdc++ for g++ 4.8.1, this constructor is missing this constructor is missing for std:unordered_map. Apart from patching the standard library headers yourself (possible, but if you have an automatic package updater, that will require constant monitoring) or submit a bug report. Microsoft did have many such issues in Visual C++ 2012 November CTP, which have all been fixed in Visual C++ 2013.

Fixes required for a conforming Standard Library

There is one big issue that will make custom allocators fragile to work with: incomplete C++11 library support. Since C++11, containers are required to go through std::allocator_traits<Allocator> to access construct() and destroy() allocator member functions, as well as to access nested typedefs such as pointer and reference. Furthermore, all containers have a constructor with a single allocator argument. Not every Standard Library implements this for every container.

With the Coliru online compiler, I can get Hinnant's stack allocator working with libstdc++ for a std::vector with only 2 minor modifications running

g++ -std=c++11 -O1 -pedantic -Wall -Wconversion -Wsign-conversion -Wsign-promo

The first is to put parenthesis around the n + (alignment - 1) operand in the align_up()

std::size_t 
 align_up(std::size_t n) noexcept
 {return (n + (alignment-1)) & ~(alignment-1);}
 ^ ^

The second is to remove the exception specification from the overloaded operator new

void* operator new(std::size_t s) // throw(std::bad_alloc)

Live Example exactly reproducing Hinnant's first test case.

Note that these 2 warnings are rather innocent and do not affect correctness of the program.

Fixes required for a non-conforming Standard Library

Since libstdc++ works fine with all containers if a std::allocator is provided (even when the container implementation directly accesses the allocator, rather than through std::allocator_traits) the safest bet is to provide all the nested typedefs and member functions of std::allocator_traits in your own allocator as well. This includes the various pointer and reference types, as well as the rebind templates. You appear to have done so, and this should resolve at least of all those issues. I still would leave the original naming of Hinnant's version in tact, though.

That leaves the issues of constructors taking a single allocator argument. At least for libstdc++ for g++ 4.8.1, this constructor is missing for std:unordered_map. Apart from patching the standard library headers yourself (possible, but if you have an automatic package updater, that will require constant monitoring) or submit a bug report. Microsoft did have many such issues in Visual C++ 2012 November CTP, which have all been fixed in Visual C++ 2013.

Fixes required for a conforming Standard Library

There is one big issue that will make custom allocators fragile to work with: incomplete C++11 library support. Since C++11, containers are required to go through std::allocator_traits<Allocator> to access construct() and destroy() allocator member functions, as well as to access nested typedefs such as pointer and reference. Furthermore, all containers have a constructor with a single allocator argument. Not every Standard Library implements this for every container.

With the Coliru online compiler, I can get Hinnant's stack allocator working with libstdc++ for a std::vector with only 2 minor modifications running

g++ -std=c++11 -O1 -pedantic -Wall -Wconversion -Wsign-conversion -Wsign-promo

The first is to put parenthesis around the n + (alignment - 1) operand in the align_up()

std::size_t 
 align_up(std::size_t n) noexcept
 {return (n + (alignment-1)) & ~(alignment-1);}
 ^ ^

The second is to remove the exception specification from the overloaded operator new

void* operator new(std::size_t s) // throw(std::bad_alloc)

Live Example exactly reproducing Hinnant's first test case.

Note that these 2 warnings are rather innocent and do not affect correctness of the program.

Fixes required for a non-conforming Standard Library

Since libstdc++ works fine with all containers if a std::allocator is provided (even when the container implementation directly accesses the allocator, rather than through std::allocator_traits) the safest bet is to provide all the nested typedefs and member functions of std::allocator_traits in your own allocator as well. This includes the various pointer and reference types, as well as the rebind templates. You appear to have done so, and this should resolve at least of all those issues. I still would leave the original naming of Hinnant's version in tact, though.

That leaves the issues of constructors taking a single allocator argument. At least for libstdc++ for g++ 4.8.1, this constructor is missing for std:unordered_map. Apart from patching the standard library headers yourself (possible, but if you have an automatic package updater, that will require constant monitoring) or submit a bug report. Microsoft did have many such issues in Visual C++ 2012 November CTP, which have all been fixed in Visual C++ 2013.

deleted 10 characters in body
Source Link
TemplateRex
  • 1.9k
  • 13
  • 22

Fixes required for a conforming Standard Library

There is one big issue that will make custom allocators fragile to work with: incomplete C++11 library support. Since C++11, containers are required to go through std::allocator_traits<Allocator> to access construct() and destroy() allocator member functions, as well as to access nested typedefs such as pointer and reference. Furthermore, all containers have a constructor with a single allocator argument. Not every Standard Library implements this for every container.

With the Coliru online compiler, I can get Hinnant's stack allocator Hinnant's stack allocator working with libstdc++ for a std::vector with only 2 minor modifications running

g++ -std=c++11 -O1 -pedantic -Wall -Wconversion -Wsign-conversion -Wsign-promo

The first is to put parenthesis around the n + (alignment - 1) operand in the align_up()

std::size_t 
 align_up(std::size_t n) noexcept
 {return (n + (alignment-1)) & ~(alignment-1);}
 ^ ^

The second is to remove the exception specification from the overloaded operator new

void* operator new(std::size_t s) // throw(std::bad_alloc)

Live Example exactly reproducing Hinnant's first test case Hinnant's first test case.

Note that these 2 warnings are rather innocent and do not affect correctness of the program.

Fixes required for a non-conforming Standard Library

Since libstdc++ works fine with all containers if a std::allocator is provided (even when the container implementation directly accesses the allocator, rather than through std::allocator_traits) the safest bet is to provide all the nested typedefs and member functions of std::allocator_traits in your own allocator as well. This includes the various pointer and reference types, as well as the rebind templates. You appear to have done so, and this should resolve at least of all those issues. I still would leave the original naming of Hinnant's version in tact, though.

That leaves the issues of constructors taking a single allocator argument. At least for libstdc++ for g++ 4.8.1, this constructor is missing for std:unordered_map. Apart from patching the standard library headers yourself (possible, but if you have an automatic package updater, that will require constant monitoring) or submit a bug report. Microsoft did have many such issues in Visual C++ 2012 November CTP, which have all been fixed in Visual C++ 2013.

Fixes required for a conforming Standard Library

There is one big issue that will make custom allocators fragile to work with: incomplete C++11 library support. Since C++11, containers are required to go through std::allocator_traits<Allocator> to access construct() and destroy() allocator member functions, as well as to access nested typedefs such as pointer and reference. Furthermore, all containers have a constructor with a single allocator argument. Not every Standard Library implements this for every container.

With the Coliru online compiler, I can get Hinnant's stack allocator working with libstdc++ for a std::vector with only 2 minor modifications running

g++ -std=c++11 -O1 -pedantic -Wall -Wconversion -Wsign-conversion -Wsign-promo

The first is to put parenthesis around the n + (alignment - 1) operand in the align_up()

std::size_t 
 align_up(std::size_t n) noexcept
 {return (n + (alignment-1)) & ~(alignment-1);}
 ^ ^

The second is to remove the exception specification from the overloaded operator new

void* operator new(std::size_t s) // throw(std::bad_alloc)

Live Example exactly reproducing Hinnant's first test case.

Note that these 2 warnings are rather innocent and do not affect correctness of the program.

Fixes required for a non-conforming Standard Library

Since libstdc++ works fine with all containers if a std::allocator is provided (even when the container implementation directly accesses the allocator, rather than through std::allocator_traits) the safest bet is to provide all the nested typedefs and member functions of std::allocator_traits in your own allocator as well. This includes the various pointer and reference types, as well as the rebind templates. You appear to have done so, and this should resolve at least of all those issues. I still would leave the original naming of Hinnant's version in tact, though.

That leaves the issues of constructors taking a single allocator argument. At least for libstdc++ for g++ 4.8.1, this constructor is missing for std:unordered_map. Apart from patching the standard library headers yourself (possible, but if you have an automatic package updater, that will require constant monitoring) or submit a bug report. Microsoft did have many such issues in Visual C++ 2012 November CTP, which have all been fixed in Visual C++ 2013.

Fixes required for a conforming Standard Library

There is one big issue that will make custom allocators fragile to work with: incomplete C++11 library support. Since C++11, containers are required to go through std::allocator_traits<Allocator> to access construct() and destroy() allocator member functions, as well as to access nested typedefs such as pointer and reference. Furthermore, all containers have a constructor with a single allocator argument. Not every Standard Library implements this for every container.

With the Coliru online compiler, I can get Hinnant's stack allocator working with libstdc++ for a std::vector with only 2 minor modifications running

g++ -std=c++11 -O1 -pedantic -Wall -Wconversion -Wsign-conversion -Wsign-promo

The first is to put parenthesis around the n + (alignment - 1) operand in the align_up()

std::size_t 
 align_up(std::size_t n) noexcept
 {return (n + (alignment-1)) & ~(alignment-1);}
 ^ ^

The second is to remove the exception specification from the overloaded operator new

void* operator new(std::size_t s) // throw(std::bad_alloc)

Live Example exactly reproducing Hinnant's first test case.

Note that these 2 warnings are rather innocent and do not affect correctness of the program.

Fixes required for a non-conforming Standard Library

Since libstdc++ works fine with all containers if a std::allocator is provided (even when the container implementation directly accesses the allocator, rather than through std::allocator_traits) the safest bet is to provide all the nested typedefs and member functions of std::allocator_traits in your own allocator as well. This includes the various pointer and reference types, as well as the rebind templates. You appear to have done so, and this should resolve at least of all those issues. I still would leave the original naming of Hinnant's version in tact, though.

That leaves the issues of constructors taking a single allocator argument. At least for libstdc++ for g++ 4.8.1, this constructor is missing for std:unordered_map. Apart from patching the standard library headers yourself (possible, but if you have an automatic package updater, that will require constant monitoring) or submit a bug report. Microsoft did have many such issues in Visual C++ 2012 November CTP, which have all been fixed in Visual C++ 2013.

added 1677 characters in body
Source Link
TemplateRex
  • 1.9k
  • 13
  • 22

Fixes required for a conforming Standard Library

There is one big issue that will make custom allocators fragile to work with: incomplete C++11 library support. Since C++11, containers are required to go through std::allocator_traits<Allocator> to access construct() and destroy() allocator member functions, as well as to access nested typedefs such as pointer and reference. Furthermore, all containers have a constructor with a single allocator argument. Not every Standard Library implements this for every container.

With the Coliru online compiler, I can get Hinnant's stack allocator working withlibstdc++ for a std::vector with only 2 minor modifications running

g++ -std=c++11 -O1 -pedantic -Wall -Wconversion -Wsign-conversion -Wsign-promo

The first is to put parenthesis around the n + (alignment - 1) operand in the align_up()

std::size_t 
 align_up(std::size_t n) noexcept
 {return (n + (alignment-1)) & ~(alignment-1);}
 ^ ^

The second is to remove the exception specification from the overloaded operator new

void* operator new(std::size_t s) // throw(std::bad_alloc)

Live Example exactly reproducing Hinnant's first test case.

The allocatorNote that you wrote above has membersthese 2 warnings are rather innocent and do not affect correctness of the program.

Fixes required for a non-conforming Standard Library

Since construct()libstdc++ andworks fine with all containers if a destroy()std::allocator. For C++11 it is not required to provide these becauseprovided (even when the container implementation directly accesses the allocator, rather than through std::allocator_traits<MyAllocator>allocator_traits) the safest bet is to provide all the nested typedefs and member functions of std::construct()allocator_traits will fall backin your own allocator as well. This includes the various pointer and reference types, as well as the rebind templates. You appear to placement-newhave done so, and this should resolve at least of all those issues. I still would leave the original naming of Hinnant's version in tact, though.

That leaves the issues of constructors taking a single allocator argument. At least for libstdc++ for g++ 4.8.1, this constructor is missing for std:unordered_map. Apart from patching the standard library headers yourself (possible, but if you don'thave an automatic package updater, that will require constant monitoring) or (andsubmit a bug report . Microsoft did have many such issues in Visual C++ 2012 November CTP, which does the Right Thing)have all been fixed in Visual C++ 2013.

With the Coliru online compiler, I can get Hinnant's stack allocator working with only 2 minor modifications running

g++ -std=c++11 -O1 -pedantic -Wall -Wconversion -Wsign-conversion -Wsign-promo

The first is to put parenthesis around the n + (alignment - 1) operand in the align_up()

std::size_t 
 align_up(std::size_t n) noexcept
 {return (n + (alignment-1)) & ~(alignment-1);}
 ^ ^

The second is to remove the exception specification from the overloaded operator new

void* operator new(std::size_t s) // throw(std::bad_alloc)

Live Example exactly reproducing Hinnant's first test case.

The allocator that you wrote above has members construct() and destroy(). For C++11 it is not required to provide these because std::allocator_traits<MyAllocator>::construct() will fall back to placement-new if you don't (and which does the Right Thing).

Fixes required for a conforming Standard Library

There is one big issue that will make custom allocators fragile to work with: incomplete C++11 library support. Since C++11, containers are required to go through std::allocator_traits<Allocator> to access construct() and destroy() allocator member functions, as well as to access nested typedefs such as pointer and reference. Furthermore, all containers have a constructor with a single allocator argument. Not every Standard Library implements this for every container.

With the Coliru online compiler, I can get Hinnant's stack allocator working withlibstdc++ for a std::vector with only 2 minor modifications running

g++ -std=c++11 -O1 -pedantic -Wall -Wconversion -Wsign-conversion -Wsign-promo

The first is to put parenthesis around the n + (alignment - 1) operand in the align_up()

std::size_t 
 align_up(std::size_t n) noexcept
 {return (n + (alignment-1)) & ~(alignment-1);}
 ^ ^

The second is to remove the exception specification from the overloaded operator new

void* operator new(std::size_t s) // throw(std::bad_alloc)

Live Example exactly reproducing Hinnant's first test case.

Note that these 2 warnings are rather innocent and do not affect correctness of the program.

Fixes required for a non-conforming Standard Library

Since libstdc++ works fine with all containers if a std::allocator is provided (even when the container implementation directly accesses the allocator, rather than through std::allocator_traits) the safest bet is to provide all the nested typedefs and member functions of std::allocator_traits in your own allocator as well. This includes the various pointer and reference types, as well as the rebind templates. You appear to have done so, and this should resolve at least of all those issues. I still would leave the original naming of Hinnant's version in tact, though.

That leaves the issues of constructors taking a single allocator argument. At least for libstdc++ for g++ 4.8.1, this constructor is missing for std:unordered_map. Apart from patching the standard library headers yourself (possible, but if you have an automatic package updater, that will require constant monitoring) or submit a bug report . Microsoft did have many such issues in Visual C++ 2012 November CTP, which have all been fixed in Visual C++ 2013.

edited body
Source Link
TemplateRex
  • 1.9k
  • 13
  • 22
Loading
added 263 characters in body
Source Link
TemplateRex
  • 1.9k
  • 13
  • 22
Loading
Source Link
TemplateRex
  • 1.9k
  • 13
  • 22
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /