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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:36:33+00:00 2026-06-10T22:36:33+00:00

while writing a offset array class(your idxs go from lets say 100 to 1000,

  • 0

while writing a offset array class(your idxs go from lets say 100 to 1000, so you create class that takes that into account without wasting first 100 slots in the array) I ran into a problem.
How to initialize a class that has an C array of elements(problem is that T doesnt have def constructor). Basically I want the array to be totally uninitiated. Example:

class MyClass
{
    MyClass(int i)
    {

    }
};
template <typename T, size_t n, size_t offset>
struct offsetedIdxArray
{
    T data[n];// error line : error C2512: 'MyClass' : no appropriate default constructor available
    offsetedIdxArray()
    {

    }
    T& operator [](size_t pos)
    {
        return data[(pos-offset)];
    }

};

usage:

offsetedIdxArray<MyClass, 1024,offset> oia;

Making def constructor is not the option because class I use is in fact library class.

*EDIT: * not related to problem described here, but it turned out that my precious library class doesnt have copy ctor, just move ctor, so I had to use vector of unique_ptr.

  • 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-06-10T22:36:35+00:00Added an answer on June 10, 2026 at 10:36 pm

    To get a statically-sized uninitialized portion of storage, you can use an “untyped” buffer of aligned storage, like std::aligned_storage<sizeof(T[n]), alignof(T)>::type in C++11 (in C++03 you need to use a char[sizeof(T[n])+something] and do manual corrections for alignment, or use a char[sizeof(T[n])] and compiler extensions to specify alignment).

    That means using placement new for constructors, and explicit destructor calls for destruction. It is up to you to track what parts of that storage have objects (and thus needs destruction) and what parts don’t have objects (and can’t have destructors called on). It is also up to you to cater for when the client requests an element that isn’t initialized at all (there’s no object at the place it’s supposed to be).

    An alternative is to use an array of boost::optionals, and then you don’t have to care about destruction and can simply assign new elements to their respective index.

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

Sidebar

Related Questions

While writing my own immutable ByteArray class that uses a byte array internally, I
While writing yet another class full of methods that access database tables to work,
While writing documentation using Sphinx, I know that I can link to a class
While writing code in a file that would comprise of PHP, HTML, CSS &
While writing a card shuffling algorithm I realized that there are 52! ~= 2^225
Sometimes while writing Java in Eclipse, I write code that generates warnings. A common
I'm writing a class for Matrix arithmetic, and one feature I'm implementing is that
Lets say I have a chat program that every time someone sends a message,
I'm writing a program that scans a text file for the byte offset of
While writing a simple library to parse a game's data files, I noticed that

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.