Thread: function template

  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 02:18 AM.

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

    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.

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

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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
    6,815
    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%.

    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.

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