Issue |
148038
|
Summary |
std::allocator cannot deallocate memory of derived class through pointer to parent class during the compile-time evaluation
|
Labels |
new issue
|
Assignees |
|
Reporter |
kridenberg
|
As far as I understand requirement of the equality of the std::allocator class it should be possible and should be well-defined. At least till pointers of the base and derived classes point to the same memory address. Example on the Compiler Explorer: https://godbolt.org/z/WcsYE94e4 and below:
```
#include <memory>
#define USE_CONSTEXPR 1
#if USE_CONSTEXPR
# define CONSTEXPR constexpr
#else
# define CONSTEXPR
#endif // USE_CONSTEXPR
struct A
{
public:
virtual ~A() noexcept = default;
public:
int a = 0;
};
struct C
{
public:
int c = 0;
};
struct B
: A
, C
{
public:
int b = 1;
};
template<typename derived_type_t>
inline CONSTEXPR auto allocate() -> derived_type_t*
{
std::allocator<derived_type_t> derived_allocator;
derived_type_t* ptr {derived_allocator.allocate(1)};
return ptr;
}
template<typename base_type_t>
inline CONSTEXPR auto deallocate(base_type_t* ptr) -> void
{
std::allocator<base_type_t> base_allocator;
base_allocator.deallocate(ptr, 1);
}
template<typename base_type_t, typename derived_type_t>
inline CONSTEXPR auto test() noexcept -> bool
{
base_type_t* base_ptr {allocate<derived_type_t>()};
deallocate<base_type_t>(base_ptr);
return true;
}
auto main() -> int
{
CONSTEXPR auto v = test<A, B>();
return 0;
}
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs