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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:30:51+00:00 2026-06-11T23:30:51+00:00

I have a template class that I am working on for a project that

  • 0

I have a template class that I am working on for a project that behaves a bit like one of the container classes in the C++ STL, but I am having some problems figuring out a good safe way to implement it.

Some background: I am trying to port an existing project called Artemis, an entity system framework, to C++ from Java. There is a class called bag that is essentially an ArrayList but the items it contains are stored in array and looked up with an index for faster access, the elements stored store their own index for the most part, but that is not really important.

The Problem: Okay, so I have a template class that contains an array of T (the template type). My issue is with returns of T elements. In my T &Get(int index) function, I want to return a reference to the element at the given index, but I do not know what to do if that element does not exist. I wanted to return something like null but that does not work since T‘s type is not guaranteed to be a pointer. I could have the container class contain T*s, but then I would hit some odd issues/complicate things for int and non-pointers (std::string) as well as some items going out of scope (unless I copied the value?).

Does anybody have any ideas or know how the built-in STL containers combat these issues?
I hope I made it clear enough what I am asking for…

Links:

  • File I am porting
  • My work so far (it is not beautiful so be warned)

File I am porting

Thank you in advance.

  • 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-11T23:30:52+00:00Added an answer on June 11, 2026 at 11:30 pm

    The standard associative containers (map, unordered_map, etc) create a default-constructed object when the [] syntax is used and return a reference to that.

    You can either do that, or throw an exception when an element is not present. The standard associative containers have an at function that does this.

    Another alternative is to have a function like the standard container’s find function which returns an iterator to the element instead of a reference to an element. If the element is not present, the iterator returned is equal to end(container). This way you can actually check if the element is present without using exceptions.

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

Sidebar

Related Questions

I have a templated container class that looks something like this: template <class ItemType>
I want to have a template class that looks something like what I have
I have a template class method like that: template<class T> static tmpClass<T>* MakeInstance(T value)
I have code that looks like this: template<class T> class list { public: class
I have a xml that looks like below <xsl:template match=//xml> <xsl:for-each select=//z:row> <ul class
I have a class library that I created using the Class Library project template.
In a project I'm working on, I have a fairly large templated class that
I have a custom iterator template class that wraps up a std::list iterator. In
I have a template class with a variadic template member function that I am
I have the following use case: There's a class called Template and with 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.