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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:59:08+00:00 2026-06-01T12:59:08+00:00

After trying to make access to a storage class a little easier, I ended

  • 0

After trying to make access to a storage class a little easier, I ended up in a situation that I don’t have a lot of knowledge on. And, finding people that are trying to do the same thing as me isn’t easy.

What I’m trying to do, is have a class that stores an array of values as strings internally, but allows simple type casting from the user’s end. What I had planned on doing is use the array subscript operator to return whichever type they specify through a template. Although, it sounds a lot better than it works in practice. Here’s a simple example of what I’m doing, to give you an idea of how it should work.

class StringList
{
    public:
    template <typename T> 
    T operator[](const int i)
}

From there, I would define a few specific templates, and any user could very easily define more if needed. But, the biggest problem with this is, I don’t know how to call the subscript operator with a template. At first I assumed the following(which apparently isn’t correct), considering it’s similar to the standard way of calling a template method.

StringList list;
T var = list<T>[0];

Does anyone know the proper way of calling the subscript operator as a template? Or, should I just avoid doing this, and use a named method?

  • 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-01T12:59:10+00:00Added an answer on June 1, 2026 at 12:59 pm

    The only way calling your operator is explicitly writing list.operator[]<T>().

    There are two basic ways out:

    1. Write a function template like list.get<int>() (as proposed by templatetypedef)
    2. Return a proxy with automatic conversation to T.

    The code would look like:

    // in the class
    struct proxy {
      proxy(StringList *list, int i) : list(list), i(i) {}
      StringList *list;
      int i;
      template <typename T>
      operator T() { return list->get<T>(i); }
    };
    
    proxy operator[](int i) { return proxy(this, i); }
    
    template <typename T> 
    T get(int i) { return ...; T(); }
    
    // how to use it:
    StringList list;
    int var = list.get<int>(0);
    float var2 = list[0];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a script that automatically starts uploading after the data has
I'm trying to make an application that allows the user to access some information
After trying to make a while(bool) loop and it failing because I couldn't see
After trying to make sense of the Metrics and Grids page of the (fairly
We are looking to make a final decision on our Mocking framework. After trying
I'm trying to make a data bound column invisible after data binding, because it
I'm trying to make an alert show three seconds after the user touches a
I am trying to make a JQGrid for a simple table. After following through
$('#div1').focus(function () { callAnotherFunction(); $(this).animate({}); } I'm trying to make $(this).animate({}); execute after callAnotherFunction();
Am trying to run pdf.js example on localhost and after downloading i did make

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.