template function instantiation

This is a discussion on template function instantiation within the C++ Programming forums, part of the General Programming Boards category; Thanks Elysia, Sorry I do not agree your below quoted text. I have made a program to make the discussion ...

  1. #16
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    Sorry I do not agree your below quoted text. I have made a program to make the discussion clear. For example, in the following code,

    1. I think f is instantised when we define function g, and T is int, deduced by the definition of g;
    2. You think it is f (100) inside main which instantise function f with parameter type t to int.

    Code:
    #include <iostream>
    
    using namespace std;
    
    template <class T> void f(T a) {g (a);}
    
    void g (int a)
    {
    	cout << a << endl;
    }
    
    int main()
    {	
    	f (100);
    	return 0;
    }
    Do I correctly describe both mine and yours ideas?

    If yes, please read the words from Bjarne, section C.13.8.3 Point of Instantiation Binding

    Code:
    template class <T> void f (T a) { g(a); }
    void g (int);
    void h()
    {
        extern g (double);
        f (2);
    }
    Bjarne said, the point of instantiation for f<int> is just before h(), so the g() called in f() is the global g(int) rather than the local g (double).

    Seems your points conflict with Bjarne's? :-)

    Any comments?

    Quote Originally Posted by Elysia View Post
    If you listen, you'll know that the compiler will not deduce a template type depending on the contents of the function itself.

    regards,
    George

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    1. I think f is instantised when we define function g, and T is int, deduced by the definition of g;
    This is wrong. g's definition does not even acknowledge the existence of f, so why would it cause its instantiation?

    Bjarne said, the point of instantiation for f<int> is just before h(), so the g() called in f() is the global g(int) rather than the local g (double).

    Seems your points conflict with Bjarne's? :-)
    Not at all. There is no relation, much less a conflict, between Elysia's and Bjarne's statements.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi CornedBee,


    I do not quite agree with you since your points conflict with Bjarne's points?

    For the following code he provided, he mentioned,

    --------------------
    the point of instantiation for f<int>() is just before h(), so the g() called in f() is the global g (int) rather than the local g (double).
    --------------------

    I think Bjarne mean the declaration of g(int) will instantise f to f <int> just before h() (I think it is called instantise a template function by its content deduction?). But your below quoted comments is, declaration of g(int) never instantising f to f<int>?

    If you think Bjarne is wrong, how do you prove it? Could you provide some pseudo code please?

    Code:
    // section C.13.8.3 Point of Instantiatant Binding
    
    template <class T> void f (T a) { g(a); }
    
    void g(int);
    
    void h()
    {
    	extern g (double);
    	f (2);
    }
    Quote Originally Posted by CornedBee View Post
    This is wrong. g's definition does not even acknowledge the existence of f, so why would it cause its instantiation?

    regards,
    George

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Bjarne's not wrong; your interpretation of what he said is. You somehow got the notion that it's g's declaration that instantiates f, which is nonsense. It's the call to f in h that instantiates f.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #20
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi CornedBee,


    If function f is instantised inside h, when we call f(2), Bjarne should say it is instantised after h(), but he said actually "the point of instantiation for f<int>() is just before h()". Please notice the word "before". It is conflicting with your point of instantiation inside h point.

    However, I do not say you are incorrect. I am also confused currently. Could you prove it is not g(int) before h(), but f(2) inside h() instantise f to f<int> pleae (with some pseudo code)?

    Quote Originally Posted by CornedBee View Post
    Bjarne's not wrong; your interpretation of what he said is. You somehow got the notion that it's g's declaration that instantiates f, which is nonsense. It's the call to f in h that instantiates f.

    regards,
    George

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You misunderstand what "point of instantiation" means. The PoI isn't the point of code that causes instantiation of the function. The PoI is where, conceptually, the generated code is placed.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #22
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    Quote Originally Posted by CornedBee View Post
    You misunderstand what "point of instantiation" means. The PoI isn't the point of code that causes instantiation of the function. The PoI is where, conceptually, the generated code is placed.
    Your description is clear. Question answered.


    regards,
    George

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deducing Function Template Arguments
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2008, 08:29 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 06:45 AM
  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

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