Download MetaTrader 5

Restricting use of template

To add comments, please log in or register
williamwong
707
williamwong  
I am trying to use template to define an array:
template <typename T>
class C1dArray
{
T m_array[];
public:
// some non computational methods
int size() { return ArraySize(m_array); }
// some computational methods
T average() { T s=0; for (int i=0;i<ArraySize(m_array);i++) s+=m_array[i]; return s/ArraySize(m_array); }
};

int start() {    C1dArray<string> a;    C1dArray<double> a;    return(0); }

This is fine when I define using C1dArray<double>.  

But when I try to define C1dArray<string>, I encounter compilation error in all the computational methods.

Is there a way to "restrict" certain methods to be defined under numeric type and other methods for all types?

fxsaber
8282
fxsaber  
template <typename T>
class C1dArray
{
T m_array[];

template <typename T1>
T1 average2( const T1& ) const
{
  T1 s=0; for (int i=0;i<ArraySize(m_array);i++) s+=m_array[i]; return s/ArraySize(m_array);
}

string average2( const string& ) const;

public:
// some non computational methods
int size() { return ArraySize(m_array); }
// some computational methods
T average() { const T Tmp = NULL; return(this.average2(Tmp)); }
};
int start() {
   C1dArray<string> a; 
   C1dArray<double> b; 
   return(0);
}
Stanislav Korotky
22141
Stanislav Korotky  
williamwong:
I am trying to use template to define an array:

This is fine when I define using C1dArray<double>.  

But when I try to define C1dArray<string>, I encounter compilation error in all the computational methods.

Is there a way to "restrict" certain methods to be defined under numeric type and other methods for all types?

No.

But you can probably create an hierarchy of classes to distinguish classes with computational capability and without it.

williamwong
707
williamwong  
fxsaber:

This will probably work for quick fix.  But if I have tons of computational logic will be quite cumbersome to do this workaround.  Thanks anyway.  You are very solid, very fast :-)
williamwong
707
williamwong  
Stanislav Korotky:

No.

But you can probably create an hierarchy of classes to distinguish classes with computational capability and without it.


Yes, probably this is the only way.  Thanks for the suggestion.

williamwong
707
williamwong  

I have created hierarchy of classes to separate numeric and string types, so far so good for 1d array.

But I encountered problem with 2darray:

//+------------------------------------------------------------------+
//| base array containing indexing, searching, sorting generic methods
//+------------------------------------------------------------------+
template<typename T>
class C1dArray
  {
protected:
   T                 m_array[];
  };
//+------------------------------------------------------------------+
//| numeric (int,double) extension to 1darray
//+------------------------------------------------------------------+
template<typename T>
class C1dNumArray:public C1dArray<T>
  {
public:
   // some mathematical methods
   T sum() { return 0; }
  };
//+------------------------------------------------------------------+
//| string extension to 1darray
//+------------------------------------------------------------------+
class C1dStrArray:public C1dArray<string>
  {
public:
   // some string methods
  };
//+------------------------------------------------------------------+
//| Jagged 2d array containing rows of 1d array
//| basic methods like sorting,searching,indexing
//+------------------------------------------------------------------+
template<typename CT>
class C2dArray
  {
protected:
   <CT>m_rows[];
  };
//+------------------------------------------------------------------+
//| numeric extension to 2d array with mathematical methods
//+------------------------------------------------------------------+
template<typename T>
class C2dNumArray:public C2dArray<C1dArray<T>>
  {
   // some mathematical methods
public:
   T sum()
     {
      T s=0;
      for(int i=0;i<ArraySize(m_rows);i++) s+=m_rows[i].sum();
      return s;
     }
  };
//+------------------------------------------------------------------+
//| string extension to 2d array with string methods
//+------------------------------------------------------------------+
class C2dStrArray:public C2dArray<C1dArray<string>>
  {
   // some string methods
public:
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   C2dNumArray<double>a;
  }
//+------------------------------------------------------------------+

Compiler complaint '<' - declaration without type mt Test type.mq4 43 4

Anything wrong?

Files:
fxsaber
8282
fxsaber  
williamwong:

Compiler complaint '<' - declaration without type mt Test type.mq4 43 4

   CT m_rows[];
williamwong
707
williamwong  
fxsaber:
oh, silly mistake I made, it worked!  Thanks.
To add comments, please log in or register