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 772813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:51:46+00:00 2026-05-14T18:51:46+00:00

I’ve written a (array) container class template (lets call it smart array ) for

  • 0

I’ve written a (array) container class template (lets call it smart array) for using it in the BREW platform (which doesn’t allow many C++ constructs like STD library, exceptions, etc. It has a very minimal C++ runtime support); while writing this my friend said that something like this already exists in Boost called MultiArray, I tried it but the ARM compiler (RVCT) cries with 100s of errors. I’ve not seen Boost.MultiArray‘s source, I’ve started learning templates only lately; template meta programming interests me a lot, although am not sure if this is strictly one that can be categorized thus.

So I want all my fellow C++ aficionados to review it ~ point out flaws, potential bugs, suggestions, optimizations, etc.; something like “you’ve not written your own Big Three which might lead to…”. Possibly any criticism that will help me improve this class and thereby my C++ skills.

Edit: I’ve used std::vector since it’s easily understood, later it will be replaced by a custom written vector class template made to work in the BREW platform. Also C++0x related syntax like static_assert will also be removed in the final code.

smart_array.h

#include <vector>
#include <cassert>
#include <cstdarg>
using std::vector;

template <typename T, size_t N>
class smart_array
{
    vector < smart_array<T, N - 1> > vec;

public:
    explicit smart_array(vector <size_t> &dimensions)
    {
        assert(N == dimensions.size());

        vector <size_t>::iterator it = ++dimensions.begin();
        vector <size_t> dimensions_remaining(it, dimensions.end());

        smart_array <T, N - 1> temp_smart_array(dimensions_remaining);
        vec.assign(dimensions[0], temp_smart_array);
    }

    explicit smart_array(size_t dimension_1 = 1, ...)
    {
        static_assert(N > 0, "Error: smart_array expects 1 or more dimension(s)");
        assert(dimension_1 > 1);

        va_list dim_list;
        vector <size_t> dimensions_remaining(N - 1);

        va_start(dim_list, dimension_1);
            for(size_t i = 0; i < N - 1; ++i)
            {
                size_t dimension_n = va_arg(dim_list, size_t);
                assert(dimension_n > 0);
                dimensions_remaining[i] = dimension_n;
            }
        va_end(dim_list);

        smart_array <T, N - 1> temp_smart_array(dimensions_remaining);
        vec.assign(dimension_1, temp_smart_array);
    }

    smart_array<T, N - 1>& operator[](size_t index)
    {
        assert(index < vec.size() && index >= 0);
        return vec[index];
    }

    size_t length() const
    {
        return vec.size();
    }
};

template<typename T>
class smart_array<T, 1>
{
    vector <T> vec;

public:
    explicit smart_array(vector <size_t> &dimension) : vec(dimension[0])
    {
        assert(dimension[0] > 0);
    }

    explicit smart_array(size_t dimension_1 = 1) : vec(dimension_1)
    {
        assert(dimension_1 > 0);
    }

    T& operator[](size_t index)
    {
        assert(index < vec.size() && index >= 0);
        return vec[index];
    }

    size_t length()
    {
        return vec.size();
    }
};

Sample Usage:

#include "smart_array.h"
#include <iostream>
using std::cout;
using std::endl;

int main()
{
    // testing 1 dimension
    smart_array <int, 1> x(3);
    x[0] = 0, x[1] = 1, x[2] = 2;
    cout << "x.length(): " << x.length() << endl;

    // testing 2 dimensions
    smart_array <float, 2> y(2, 3);
    y[0][0] = y[0][1] = y[0][2] = 0;
    y[1][0] = y[1][1] = y[1][2] = 1;
    cout << "y.length(): " << y.length() << endl;
    cout << "y[0].length(): " << y[0].length() << endl;

    // testing 3 dimensions
    smart_array <char, 3> z(2, 4, 5);
    cout << "z.length(): " << z.length() << endl;
    cout << "z[0].length(): " << z[0].length() << endl;
    cout << "z[0][0].length(): " << z[0][0].length() << endl;
    z[0][0][4] = 'c'; cout << z[0][0][4] << endl;

    // testing 4 dimensions
    smart_array <bool, 4> r(2, 3, 4, 5);
    cout << "r.length(): " << r.length() << endl;
    cout << "r[0].length(): " << r[0].length() << endl;
    cout << "r[0][0].length(): " << r[0][0].length() << endl;
    cout << "r[0][0][0].length(): " << r[0][0][0].length() << endl;

    // testing copy constructor
    smart_array <float, 2> copy_y(y);
    cout << "copy_y.length(): " << copy_y.length() << endl;
    cout << "copy_x[0].length(): " << copy_y[0].length() << endl;

    cout << copy_y[0][0] << "\t" << copy_y[1][0] << "\t" << copy_y[0][1] << "\t" << 
        copy_y[1][1] << "\t" << copy_y[0][2] << "\t" << copy_y[1][2] << endl;

    return 0;
}
  • 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-14T18:51:47+00:00Added an answer on May 14, 2026 at 6:51 pm

    If I’m understanding what you want from this type:

    In short, it would be optimal to use the form:


    template < typename T_, unsigned N_ >
    struct t_array {
    /* ... */
    static const size_t Size = N_;
    typedef T_ T;
    T objects_[Size];
    };

    for many reasons if you want only a fixed size and fixed type array. The compiler can make a lot of safe assumptions – this has reduced object size to 20% (compared to using std::vector) for me in some cases. It also faster, safer. If you use them everywhere, then you may end up creating much larger binaries (compared to using std::vector).

    There is a class <boost/array.hpp> which you should read.

    Sorry if you don’t find that helpful – I think reading at least one common production quality implementation (before venturing into new technologies) would help.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Maybe this one (adapting it to your language, of course)… May 15, 2026 at 9:59 am
  • Editorial Team
    Editorial Team added an answer $('li').click(function(event) { //do something event.stopPropagation() }); This will work in… May 15, 2026 at 9:59 am
  • Editorial Team
    Editorial Team added an answer There's computational overhead and complexity associated with sparse representations, so… May 15, 2026 at 9:59 am

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.