Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Array Subscript Operator Template

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?

like image 272
TheCodeBroski Avatar asked Mar 31 '12 17:03

TheCodeBroski


People also ask

What is subscript operator in array?

A postfix expression followed by an expression in [ ] (brackets) specifies an element of an array. The expression within the brackets is referred to as a subscript. The first element of an array has the subscript zero.

What is subscript operator in C?

A postfix expression (operand followed by the operator) followed by an expression in square brackets [ ] is a subscripted designation of the array element . The definition of the array subscript operator [ ] is that if a is an array and i is an integer then a[i] =*(a+i) .

What are the limitations of subscript operator in array?

An array doesn't check boundaries: In C language, we cannot check, if the values entered in an array are exceeding the size of that array or not. Data that is entered with the subscript, exceeds the array size and will be placed outside the array.

How you load the subscript operator explain?

Subscripting [] Operator Overloading in C++ The subscript operator [] is normally used to access array elements. This operator can be overloaded to enhance the existing functionality of C++ arrays.


2 Answers

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];
like image 91
ipc Avatar answered Oct 18 '22 03:10

ipc


I think there is no syntax to pass template parameters to the natural call of operator[]. You would probably need to call:

T var = list.operator[]<T>(0);

Like you do with normal template functions, so there is no point in using operator overload here.

like image 22
lvella Avatar answered Oct 18 '22 03:10

lvella