Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 939795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:50:56+00:00 2026-05-15T21:50:56+00:00

I’m looking for a C++ container-like class that wraps a typed array of objects

  • 0

I’m looking for a C++ container-like class that wraps a typed array of objects that are not necessarily initialized and don’t have to be default-constructible or copy-constructible. This would be interesting for RAII objects that have no well-defined copy semantics. Such a container-like class seems to be fairly easy to write (using an allocator to allocate uninitialized memory and placement new). Is there something like this in Boost that I have just overlooked? I’m not looking for std::vector (which requires its elements to be copy-constructible) or a pointer container, but for something like this:

#include <cstddef>
#include <memory>
#include <vector>
#include <algorithm>
#include <iostream>


template< typename T, typename Alloc = std::allocator<T> >
class FixedVector {
public:
  typedef typename Alloc::value_type value_type;
  typedef typename Alloc::pointer pointer;
  typedef typename Alloc::reference reference;
  typedef typename Alloc::const_pointer const_pointer;
  typedef typename Alloc::const_reference const_reference;
  typedef typename Alloc::size_type size_type;
  typedef typename Alloc::difference_type difference_type;
  typedef pointer iterator;
  typedef const_pointer const_iterator;

  explicit FixedVector(size_type size, const Alloc& allocator = Alloc()):
    m_alloc(allocator),
    m_size(size),
    m_data(m_alloc.allocate(size)),
    m_constructed(size) { }

  FixedVector(const FixedVector& other):
    m_alloc(other.m_alloc),
    m_size(other.m_size),
    m_data(m_alloc.allocate(m_size)),
    m_constructed(other.m_constructed) {
    for (size_type i = 0; i != m_size; ++i) {
      if (m_constructed[i]) m_alloc.construct(m_alloc.address(m_data[i]), other[i]);
    }
  }

  ~FixedVector() {
    for (size_type i = 0; i != m_size; ++i) {
      if (m_constructed[i]) m_alloc.destroy(m_alloc.address(m_data[i]));
    }
    m_alloc.deallocate(m_data, m_size);
  }

  FixedVector& operator=(FixedVector other) {
    other.swap(*this);
    return *this;
  }

  // operator[] and other unimportant stuff

  void swap(FixedVector& other) {
    std::swap(m_alloc, other.m_alloc);
    std::swap(m_size, other.m_size);
    std::swap(m_data, other.m_data);
    std::swap(m_constructed, other.m_constructed);
  }

  void construct(size_type index) {
    new (m_alloc.address(m_data[index])) T();
    m_constructed[index] = true;
  }

  template<typename U>
  void construct(size_type index, U& val) {
    new (m_alloc.address(m_data[index])) T(val);
    m_constructed[index] = true;
  }

  template<typename U>
  void construct(size_type index, const U& val) {
    new (m_alloc.address(m_data[index])) T(val);
    m_constructed[index] = true;
  }

private:
  Alloc m_alloc;
  size_type m_size;
  pointer m_data;
  std::vector<bool> m_constructed;
};


template<typename T, typename Alloc>
void swap(FixedVector<T, Alloc>& first, FixedVector<T, Alloc>& second) {
  first.swap(second);
}


namespace std {
  template<typename T, typename Alloc>
  void swap(FixedVector<T, Alloc>& first, FixedVector<T, Alloc>& second) {
    first.swap(second);
  }
}


class Test {
public:
  explicit Test(int val): m_val(val) {
    std::cout << "Test::Test(" << val << ')' << std::endl;
  }

  ~Test() {
    std::cout << "Test::~Test() [with m_val = " << m_val << ']' << std::endl;
  }

  int val() const {
    return m_val;
  }

private:
  int m_val;

  Test(const Test&);
  Test& operator=(const Test&);
};

template<typename Char, typename Traits>
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& stream, const Test& object) {
  return stream << object.val();
}


int main() {
  typedef FixedVector<Test> FVT;
  FVT w(10);
  w.construct(7, 7);
  w.construct(2, 2);
  std::cout << "w[2] = " << w[2] << std::endl;
}

The solution should work in C++03 (e.g. no move semantics allowed). The question is a bit academical—I’m just wondering why such a class doesn’t seem to exist in Boost.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:50:56+00:00Added an answer on May 15, 2026 at 9:50 pm

    Such a container-like class seems to
    be fairly easy to write (using an
    allocator to allocate uninitialized
    memory and placement new).

    And that is exactly what std::vector does. To use placement new, you would have to make a copy.

    void store(const T& value)
    {
        new (storage) T(value); //<-- invokes copy constructor
    }
    

    Perhaps boost::ptr_vector would work for non-copyable types (you’d give it pointers).

    #include <boost/noncopyable.hpp>
    #include <boost/ptr_container/ptr_vector.hpp>
    #include <iostream>
    
    struct X: boost::noncopyable
    {
        X(int x): x(x) {}
        int x;
    };
    
    int main()
    {
        boost::ptr_vector<X> vec;
        for (int i = 1; i < 10; ++i) {
            vec.push_back(new X(i));
        }
    
        for (size_t i = 0; i != vec.size(); ++i) {
            std::cout << vec[i].x << '\n';
        }
    }
    

    And in C++0x, containers will accept non-copyable types as long as they are movable (which should normally be implementable for non-copyable types).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 460k
  • Answers 460k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not entirely a regular expression. The regex is… May 15, 2026 at 11:50 pm
  • Editorial Team
    Editorial Team added an answer using this code we can do it simply public string… May 15, 2026 at 11:50 pm
  • Editorial Team
    Editorial Team added an answer The PList class from code.google.com/xmlwise looks more promising to me. May 15, 2026 at 11:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.