skip to Main Content

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.

vscode result

xcode result

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.
solution

2

Answers


  1. The member functions construct and destroy 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.

    Login or Signup to reply.
  2. 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:

    using allocator_traits = std::allocator_traits<std::allocator<int>>;
    
    allocator_traits::allocator_type a;
    
    allocator_traits::size_type size = 1;
    
    auto var = allocator_traits::allocate(a, size);
    allocator_traits::construct(a, var, 10);
    allocator_traits::destroy(a, var);
    allocator_traits::deallocate(a, size);
    

    std::allocator‘s construct and destroy member functions have been removed with C++20 and now also use the defaults provided by std::allocator_traits instead, in line with the convention that std::allocator_traits is the actual interface to allocators.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search