Re: template for function pointer

From: Thomas Wintschel (thomas_at_portal.ca)
Date: 01/02/05

  • Next message: Charlie Zender: "Re: Assignment to std::complex number"
    Date: Sun, 02 Jan 2005 01:50:46 GMT
    
    

    <firegun9@yahoo.com.tw> wrote in message
    news:1104616356.358972.83090@z14g2000cwz.googlegroups.com...
    > Hello everyone,
    > here is my program:
    >
    > ///////////////////////////////
    > #include <iostream>
    > using namespace std;
    >
    > void multi(double* arrayPtr, int len){
    > for(int i=0; i<len; i++)
    > *(arrayPtr+i)*=2;
    > }
    > typedef void (*p2f) (double* a, int b);
    > //////////////////////////////
    > template <class T>
    > class bf{
    > private:
    > int type;
    > T var;
    > public:
    > bf(int i, T value){ type=i; var=value;}
    > void eval(double* dPtr, int len);
    > };
    >

    Generally, you use a template when you want to apply the same code
    structure to different types. Your problem here is that you are trying
    to apply different code depending on the type which fails because you
    cannot provide a T that has all the necessary properties.

    > template <class T>
    > void bf<T>::eval(double* dPtr, int len){
    > switch(type){
    > case 0:
    > for(int i=0; i<len; i++)
    > *(dPtr+i)=var;

    In order for this line to compile, it must be possible to convert T to a
    double.

    > break;
    > case 1:
    > for(int i=0; i<len; i++)
    > *(dPtr+i)=*(var+i);

    In order for this line to compile, it must be possible to convert T to a
    double*.

    > break;
    > case 2:
    > var(dPtr, len);

    In order for this line to compile,
    T must have a constructor that takes a double* and an int.

    > break;
    > }
    > }
    >

    So, if you want the preceding method to compile, you need to use a T for
    which the following statements are all valid:

    T t1;
    double d = t1;
    double* p = t1;
    T t2(p, 1);

    > void main(){
    > double a=0.0;
    > bf<double> a1(0,a);
    >
    > double b[3]={0.0, 1.0, 2.0};
    > bf<double*> a2(1,b);
    >
    > bf<p2f> a3(2,multi);
    >
    > double temp[3]={5.0, 5.0, 5.0};
    > a1.eval(temp,3);
    > for(int i=0;i<3;i++)
    > cout<<temp[i]<<endl;
    > }
    >
    > I have a bf class with a member variable "var" whose type is defined
    as
    > a template. In bf's constructor, I use (int type) to record what type
    > var is defined.
    > The main purpose of bf is to retrieve a double array, then modified it
    > using the var member.
    > By different types of var, it does different modifies to the input
    > array.
    >
    > There are 3 conditions:
    > If var is a double( it can be known from "type" variable), it fill the
    > incoming array with the double value.
    > If var is a double array, it copies its value one by one into the
    > incoming array.
    > If var is a function pointer, it takes the incoming array as the
    > argument.
    >
    > There's no problem untill I call bf::eval() in main();
    > It seems like that if var is a double then the code "var(dPtr, len);"
    > is wrong. In fact, it is wrong. But that is in case 2. Whenever the
    var
    > is not a function pointer, it will never go into case 2 in the switch
    > in eval();
    > The familiar situation also happens when the var is set to a function
    > pointer. The error was found in case 0, in which var is treated as a
    > double.
    >
    > I just wanna control the branch call by myself.
    > Is there any solution?
    > Thanks
    >

    I will ignore other obvious, dangerous errors in your code since I think
    you are really misguided in trying to do what you are doing.

    Some advice:

    First read some more about C++ and object-oriented programming. You are
    trying to treat numerical, pointer and function pointer types like they
    are the same thing when they are very different creatures.

    Read much more about templates. In particular, take a look at STL type
    requirements, which specify the properties a type must have in order to
    be used with a particular template. A vector, for instance, requires
    that T be 'Assignable' meaning that you can write:
    T a;
    T b = a;

    Perhaps if you state what higher level problem you are trying to solve
    you will get some useful advice. In the meantime, remember that
    templates will only work with types that have something in common.

    Merry New Year
    Tom


  • Next message: Charlie Zender: "Re: Assignment to std::complex number"

    Relevant Pages

    • Re: check to see if value can be an integer instead of string
      ... > have looked at is int, but what is comming out is a string that may ... The "int" builtin function never returns any value but an integer. ... > var = some var passed to my script ...
      (comp.lang.python)
    • Re: template for function pointer
      ... my response was not to Thomas but to the original poster. ... > The problem you are seeing is that the compiler is trying to compile all ... >> using the var member. ... >> incoming array with the double value. ...
      (comp.lang.cpp)
    • Re: Passing const int * to a function
      ... For var to be constant the declaration of f should be ... int f ... int f (const int *const var) ... Bounce first left and then right and then continue in this same ...
      (comp.lang.c)
    • Re: Postfix increment
      ... specification doesn't contain a strict evaluation order of arguments. ... Now that we have that we can look at the difference between var++ and ... int varPlusPlus ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: template for function pointer
      ... three branches of the case statement *for each type of the template ... I use (int type) to record what type ... > var is defined. ... > incoming array with the double value. ...
      (comp.lang.cpp)