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

  • Home
  • SEARCH
  • 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 7059369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:13:26+00:00 2026-05-28T04:13:26+00:00

I am creating a templated Vector class, however, when comparing its use to something

  • 0

I am creating a templated Vector class, however, when comparing its use to something such as std::vector, I noticed that it does not allow structs\classes without a default (emtpty) constructor. The error I will get is

error C2512: 'SomeStruct' : no appropriate default constructor available
  : while compiling class template member function 'Vector<Type>::Vector(void)'
  : see reference to class template instantiation 'Vector<Type>' being compiled

However, if I were to go and use std::vector, this would be allowed. Here is my test case

struct SomeStruct
{
    SomeStruct(int a){}
};
template<typename Type>
class Vector
{
public:
    Vector();

protected:
    Type* m_Data;
    unsigned int m_Count;
    unsigned int m_Capacity;
};
template<typename Type>
Vector<Type>::Vector()
{
    m_Capacity = 0;
    m_Count = 0;
    m_Data = new Type[m_Capacity];
}

void main()
{
    Vector<SomeStruct> test1;
}

How can I allow my templated Vector allow types without a default (empty) constructor?

(I know I could just use std::vector, but I am doing this to learn more about the language, and to run into cases like this)

  • 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-28T04:13:27+00:00Added an answer on May 28, 2026 at 4:13 am

    The reason why this doesn’t work for types without default constructors is because of this line:

        m_Data = new Type[m_Capacity]; 
    

    The above line fundamentally does two things: allocate enough memory to hold m_Capacity instances of Type, then constructing each Type so that they’re ready to use. Since you can’t actually provide any constructor arguments through this new[] syntax, default constructors are required when you use this.

    The way std::vector (and the other standard containers) deals with this is by separating the memory allocation process and the construction process. That is, std::vector amortizes the cost of memory allocation by requesting large chunks of memory with “nothing” in it. Then std::vector uses placement new to construct objects directly in that memory.

    So something like this might be going on inside a std::vector:

    // HUGE SIMPLICATION OF WHAT HAPPENS!!!
    // EXPOSITION ONLY!!!
    // NOT TO BE USED IN ANY PRODUCTION CODE WHATSOEVER!!!
    // (I haven't even considered exception safety, etc.)
    
    template<typename T>
    class Vector
    {
    private:
        T* allocate_memory(std::size_t numItems)
        {
            // Allocates memory without doing any construction
            return static_cast<T*>(::operator new(sizeof(T)*numItems));
        }
    
        void deallocate_memory()
        {
            ::operator delete(buffer);
        }
        // ...
    
    public:
        void push_back(const T& obj)
        {
            if(theresNotEnoughRoom()) {
                std::size_t newCapacity = calculateNewCapacity();
                T* temp = allocate_memory(newCapacity);
                copyItemsToNewBuffer(temp);
                deallocate_memory(buffer);
                buffer = temp;
                bufferEnd = temp+newCapacity;
            }
            new (bufferEnd) T(obj); // Construct a new instance of T at end of buffer.
            ++bufferEnd;
        }
    
        void pop_back()
        {
            if(size() > 0) {
                --bufferEnd;
                bufferEnd->~T();
            }
        }
    
        // ...
    
    private:
        T* buffer;
        T* bufferEnd;
        // ...
    };
    

    So what’s going on here is that our hypothetical Vector class allocates a relatively large slab of memory, then as items are pushed or inserted, the class does placement new in the memory. So this eliminates the default constructor requirement, since we don’t actually construct any objects unless requested by the caller.

    As you can already see, a std::vector class needs to do quite a bit of bookkeeping to make what it does efficient and safe. That’s why we urge people to use the standard containers instead of rolling out your own, unless you really know what you’re doing. Making an efficient, safe, and useful vector class is a huge undertaking.

    For an idea of what’s involved, take a look at a paper called “Exception Safety: Concepts and Techniques” by Bjarne Stroustrup which discusses a “simple vector” implementation (section 3.1). You’ll see that it’s not a trivial thing to implement.

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

Sidebar

Related Questions

I'm creating dynamic templated XAML designs that I would like to convert to PNG
I could use some help creating an XSL template that will take a string
I'm creating a new class that inherits queue from the STL library. The only
I am creating a templated class D<N> , with a method (operator(), in this
I overheard sometime ago a discussion about how when creating a templated string class
I have a class for logging, which MUST NOT inherit std::ostream, and has operator<<
I am creating my own custom T4 Template that integrates with an ADO.NET Entity
I'm trying to create a Vector class using templates to store 3D positions of
I'm creating a custom templated composite control. If there is no 'ItemTemplate' specified in
Using the advise given on this post... Creating an ASP.Net Templated Server control ...

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.