function template

This is a discussion on function template within the C++ Programming forums, part of the General Programming Boards category; Hi all, I have a few points about function template that I need to clear up. So i have this ...

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

    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    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
    452
    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
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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-20-2010 at 11:41 PM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    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*.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,310
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    452
    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-20-2010 at 11:59 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,166
    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%.

  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
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
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, 01:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01: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, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21