This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Wish that templates where more complete.


I am using gcc-2.95.2 under Linux 2.2.14 glibc-2.1.

I am attempting to create templated typedefs. I am unable to craete templated
typedefs.

//======SNIP /*I can write it like this*/=====================
#include <iostream.h>

typedef bool(*IntegerHandler)(int &);
typedef bool(*FloatHandler)(float &);

template <class datatype>
class test1class {
  protected:
    datatype data;
    bool (*datahandler)(datatype &);
  public:
    test1class(const datatype &d, bool (*handler)(datatype &) = NULL)
      { data = d; datahandler = handler;  }
    ~test1class() {}
    bool processdata(void)
      { return datahandler ? datahandler(data) : false; }
};

bool process1(int &i)
{
  if (i==0)
  {
    cout << "Data is 0.\n";
    i=1;
    return false;
  } else
  {
    cout << "Data is not 0.\n";
    i=0;
    return true;
  }
}

bool process2(float &f)
{
  if (f<0.0)
  {
    cout << "Data is Less than 0.0.\n";
    f=1.0;
    return false;
  } else
  {
    cout << "Data is Greater than 0.0.\nb";
    f=0.0;
    return true;
  }
}

int main(void)
{
  IntegerHandler handler1 = process1;
  FloatHandler handler2 = process2;
  test1class<int> test1(1,handler1);
  test1class<float> test2(1.0,handler2);
  cout << "test1.processdata() returned '"
    << (test1.processdata() ? "true" : "false")
    << "'.\n";
  cout << "test1.processdata() returned '"
    << (test1.processdata() ? "true" : "false")
    << "'.\n";
  cout << "test2.processdata() returned '"
    << (test2.processdata() ? "true" : "false")
    << "'.\n";
  cout << "test2.processdata() returned '"
    << (test2.processdata() ? "true" : "false")
    << "'.\n";
  return 0;
}
//======SNIP==================================================

//======SNIP /*I would prefer to write it like this*/=========
//This is much clearer to understand and use.
#include <iostream.h>

template <class datatype>
typedef bool(*DataHandler)(datatype &);

template <class datatype>
class test1class {
  protected:
    datatype data;
    DataHandler<datatype> datahandler;
  public:
    test1class(const datatype &d, DataHandler<datatype> handler = NULL)
      { data = d; datahandler = handler;  }
    ~test1class() {}
    bool processdata(void)
      { return datahandler ? datahandler(data) : false; } 
};

bool process1(int &i)
{
  if (i==0)
  {
    cout << "Data is 0.\n";
    i=1;
    return false;
  } else
  {
    cout << "Data is not 0.\n";
    i=0;
    return true;
  }
}

bool process2(float &f)
{
  if (f<0.0)
  {
    cout << "Data is Less than 0.0.\n";
    f=1.0;
    return false;
  } else
  {
    cout << "Data is Greater than 0.0.\nb";
    f=0.0;
    return true;
  }
}

int main(void)
{
  DataHandler<int> handler1 = process1;
  DataHandler<float> handler2 = process2;
  test1class<int> test1(1,handler1);
  test1class<float> test2(1.0,handler2);
  cout << "test1.processdata() returned '"
    << (test1.processdata() ? "true" : "false")
    << "'.\n";
  cout << "test1.processdata() returned '"
    << (test1.processdata() ? "true" : "false")
    << "'.\n";
  cout << "test2.processdata() returned '"
    << (test2.processdata() ? "true" : "false")
    << "'.\n";
  cout << "test2.processdata() returned '"
    << (test2.processdata() ? "true" : "false")
    << "'.\n";
  return 0;
}
//======SNIP==================================================

-- 
Jose Santiago

Senior Systems Analyst - Scientific Systems
Komatsu Mining Systems - Peoria Operations
2300 N.E. Adams Street
P.O. Box 240
Peoria, IL 61650-0240

Voice:309-672-7325  Fax:309-672-7753
mailto:jose@haulpak.com

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]