I have a MacBook Air and I’m using Xcode as my IDE. When I write the code below, Xcode shows a warning that says "no member named destroy and construct in std::allocator<int>
". However, when I try to compile the same code in VS Code on my Mac, it builds successfully without any warnings.
#include <memory>
int main(){
std::allocator<int> a;
int* var = a.allocate(1);
a.construct(var,10);
a.destroy(var);
}
Please let me know if there are any other issues with the statement that you would like me to address.
I’m wondering why the C++ library is not consistent across IDEs, since I assumed it should be the same. Please help me with it, thank you!
I changed the C++ language dialect to C++11, and that solved the problem.
2
Answers
The member functions
construct
anddestroy
were deprecated in C++17 and removed in C++20.Presumably you’re using a C++20 compiler in Xcode (hence those member functions are not available) while in VS Code you’re using an older standard.
Since C++11, you are not supposed to call member functions on an allocator type directly. Instead always go through
std::allocator_traits
, which defaults them if they are not explicitly provided by the allocator type:std::allocator
‘sconstruct
anddestroy
member functions have been removed with C++20 and now also use the defaults provided bystd::allocator_traits
instead, in line with the convention thatstd::allocator_traits
is the actual interface to allocators.