Thread: function template

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463

    function template

    Hi all,
    I have a few points about function template that I need to clear up. So i have this array in main.
    Code:
    double *array;
    And I want to pass that one into a function template.
    Code:
    // version 1
    template<class Type>
    void sort(Type & A, int length)
    //version 2
    template<class Type>
    void sort(Type * A, int length)
    If i use the version 1 to declare the template, sort(array,length). that would make A a reference to double *, and Type=double *.
    If i use version 2 , then A is a pointer to type double, and Type = double.
    I tested out both template, and they both worked. But I want to ask you guys's opnions of which version is a better coding practice? or are they the same as long as I use Type consistently in the function body?

    One more point that I am kind of confused is that if I use an explicit specialization for the function template, wouldn't it be the same as using a nontemplate function?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think calling sort<double*>(array, n); given the real type of array, uses a different type for no reason. A pointer is already a pass-by-reference type, which is all you could gain from the overloaded functions.

    Template specialization is important for when certain template arguments mean the template code itself needs to be different.

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Thanks whiteflags.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    Since array is double * in both cases, how does Type end up being double in version 2?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nextstopearth
    Since array is double * in both cases, how does Type end up being double in version 2?
    For version 2, the first parameter is of type Type*. Therefore, if the corresponding argument is of type double*, it follows that Type is double.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    Quote Originally Posted by laserlight View Post
    For version 2, the first parameter is of type Type*. Therefore, if the corresponding argument is of type double*, it follows that Type is double.
    I am confused then. If Type is double *, and we have Type *A, it seems like A would be a double **.
    Last edited by Nextstopearth; 12-21-2010 at 12:41 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nextstopearth
    If Type is double *, and we have Type *A, it seems like A would be a double **.
    Yes, hence Type is double, not double*.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    Quote Originally Posted by laserlight View Post
    Yes, hence Type is double, not double*.
    That doesn't help me...I know Type is supposed to be double. I just don't see why. Again, if you are sending a double * as an argument, and then declaring a pointer to that, how is A not a pointer to a double *?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nextstopearth
    That doesn't help me...I know Type is supposed to be double. I just don't see why. Again, if you are sending a double * as an argument, and then declaring a pointer to that, how is A not a pointer to a double *?
    Looking only at version 2 of the function template, what is the type of the parameter A?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    I think since "array" is a pointer to double, when you pass into the function, A becomes a pointer to double. Thus that makes Type = double. doesn't template look at the argument before specialization?
    Last edited by nimitzhunter; 12-21-2010 at 12:59 AM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You want to pass a double * to your version 2 template function with this signature
    Code:
    template<class Type> sort(Type *, int);
    This means the sort() function, obtained by instantiating your version 2 template, must accept a double * as first argument. For "Type *" and "double *" to be the same type, Type must be double.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    Quote Originally Posted by laserlight View Post
    Looking only at version 2 of the function template, what is the type of the parameter A?
    Type *

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nextstopearth
    Type *
    Indeed. So if A is of type Type*, and if array is of type double*, then Type is double. This is extremely simple pattern matching.

    Using your language: if you are sending a double* as an argument, and then declaring a pointer to a type named Type, then Type must be a double. It cannot possibly be a double*, because you are declaring a pointer to Type, not a value or reference of type Type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    Quote Originally Posted by laserlight View Post
    Indeed. So if A is of type Type*, and if array is of type double*, then Type is double. This is extremely simple pattern matching.

    Using your language: if you are sending a double* as an argument, and then declaring a pointer to a type named Type, then Type must be a double. It cannot possibly be a double*, because you are declaring a pointer to Type, not a value or reference of type Type.
    Well apparently there are things going on here that I am not aware of. For example:

    Code:
    template <typename T>
    void func(T arg);
    If you call func with
    Code:
    func(3);
    You just replace T with the type of the argument, which would be int. In the other example in this thread you don't just replace the template parameter with the exact type of the actual argument, something more happens. I didn't know that.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nextstopearth
    In the other example in this thread you don't just replace the template parameter with the exact type of the actual argument, something more happens.
    Yes. Let's consider this again:
    Code:
    template <typename T>
    void func(T arg);
    Now, you write:
    Code:
    double* p;
    func(p);
    So, T is double*. But now we change it to:
    Code:
    template <typename T>
    void func(T* arg);
    If T is still double*, then it follows that the * has changed nothing. In other words, using your reasoning, this:
    Code:
    template <typename T>
    void func(T arg);
    and this:
    Code:
    template <typename T>
    void func(T**************************************** arg);
    still result in T being of type double* if the argument is of type double*. Does that make sense to you? It does not make sense to me.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM