function template

This is a discussion on function template within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by laserlight If T is still double*, then it follows that the * has changed nothing. In other ...

  1. #16
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    Quote Originally Posted by laserlight View Post
    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.
    For that last function declaration, my reasoning would be that, if a double * was passed to it,

    T **************************************** arg would now actually be
    double * **************************************** arg


    But you are telling me that for:
    Code:
    template <typename T>
    void func(T* arg);
    
    double* p;
    func(p);
    T is of type double. But now what if ones calls func with a double argument, T is still a double?
    Last edited by Nextstopearth; 12-21-2010 at 01:18 AM.

  2. #17
    Registered User
    Join Date
    Jun 2005
    Posts
    5,884
    Quote Originally Posted by Nextstopearth View Post
    But now what if ones calls func with a double argument, T is still a double?
    The result will be a compilation error. All instantiations of func() accept a pointer to something. A double is not a pointer, so cannot be passed to any functions obtained by instantiating this template.

    To spell it out, if you try;
    Code:
    template <typename T>
    void func(T* arg) {};
    
    int main()
    {
        double p = 2.0;
        func(p);
    }
    The compiler will complain bitterly as p is not a pointer type.
    Right 98% of the time, and don't care about the other 3%.

  3. #18
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    Ahh, I get it now, thanks.

  4. #19
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    Quote Originally Posted by nimitzhunter View Post
    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?
    There is a difference between template initialization, which you are talking about mainly in this post and this thread, and template specialization. In template specialization, you are providing a whole new implementation when an argument is a particular type.

    Suppose that template foo works for all types except char where it should be different. Rather than abandon templates altogether, you could provide code for the special case.
    Code:
    template<typename T> class foo
    {
        ... 
    };
    
    template<> class Foo<char>
    {
        ...
    };
    Hence, my earlier statement:
    Template specialization is important for when certain template arguments mean the template code itself needs to be different.
    Like I said, you are talking about template initialization, and I personally only provide that explicitly if I need to provide it. There are situations where there is nothing to infer, or the compiler calls the wrong thing thanks to someone's reliance on implicit conversions between types, but that's it. I should have said something sooner but I was in a literal state of mind.

  5. #20
    Registered User
    Join Date
    Jun 2005
    Posts
    5,884
    Quote Originally Posted by whiteflags View Post
    There is a difference between template initialization, which you are talking about mainly in this post and this thread, and template specialization.
    I believe what you're referring to as "template initialization" is more commonly known as "template instantiation".
    Right 98% of the time, and don't care about the other 3%.

Page 2 of 2 FirstFirst 12
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