I found that the pmr::polymorphic_allocator type seems to be not properly detected at compilation type.
I've the class (for example MyClass), what uses pmr::polymorphic_allocator, and this allocator has to be passed by constructor parameter:
// file MyClass.hpp
#include <memory_resource>
class Item
{
// class members...
};
class MyClass
{
public:
explicit MyClass(std::pmr::polymorphic_allocator<Item> allocator)
{
// ....
}
// other members
};
Then, I'm trying to use my class:
#include "MyClass.hpp"
#include <memory_resource>
// this compiles w/out errors - as expected
auto myClass1 = MyClass(std::pmr::polymorphic_allocator<Item>());
// and this compiles w/out errors too - as NOT expected
auto myClass2 = MyClass(std::pmr::polymorphic_allocator<OtherType>());
Off course, the type OtherType is totally different than type Item.
So, I added some additional checks in constructor of MyClass:
// file MyClass.hpp
#include <memory_resource>
#include <type_traits>
class Item
{
// class members...
};
class MyClass
{
public:
explicit MyClass(std::pmr::polymorphic_allocator<Item> allocator)
{
static_assert(std::is_same<Item, decltype(allocator)::value_type>::value,
"Improper allocator!");
// ....
}
// other members
};
... and the improper allocator is still not detected. I can't find any reason for that. Any ideas?
My environment: GCC 10.2.0, CLion 2021.1
EDIT: ok, I found the reason.
The pmr::polymorphic_allocator has converting constructor, so there occurs the implicit conversion to the expected allocator:
constructor description on cppreference.com.
So, it seems that there is no way to detect the usage of improper allocator.