How many times will a template function be instantiated for the same parameter?

Discussion in 'C++' started by Shriramana Sharma, May 28, 2013.

  1. Hello. I have this global operator I'm defining in my app for convenience:

    # include <vector>
    using namespace std ;
    template < typename T >
    vector<T> & operator << ( vector<T> & vec, const T & val ) { vec.push_back(val) ; return vec ; }

    This is so I can do something like:

    vector<int> A ;
    A << 1 << 2 << 3 ;

    My question is how often will this function be instantiated? I mean, if I had written it like:

    vector<int> & operator << ( vector<int> & vec, const int & val ) { vec.push_back(val) ; return vec ; }

    then there would be only one definition and this function would be called each time I use the operator. However I'm using a template to be able to usethis for a vector of any value type. So I would like to know, how many times would a particular template be instantiated? Would it also be one in accordance with the ODR or something?
    Shriramana Sharma, May 28, 2013
    #1
    1. Advertising

  2. Shriramana Sharma

    Ian Collins Guest

    Re: How many times will a template function be instantiated for thesame parameter?

    Shriramana Sharma wrote:
    > Hello. I have this global operator I'm defining in my app for
    > convenience:
    >
    > # include <vector> using namespace std ; template < typename T >
    > vector<T> & operator << ( vector<T> & vec, const T & val ) {
    > vec.push_back(val) ; return vec ; }
    >
    > This is so I can do something like:
    >
    > vector<int> A ; A << 1 << 2 << 3 ;
    >
    > My question is how often will this function be instantiated? I mean,
    > if I had written it like:
    >
    > vector<int> & operator << ( vector<int> & vec, const int & val ) {
    > vec.push_back(val) ; return vec ; }
    >
    > then there would be only one definition and this function would be
    > called each time I use the operator. However I'm using a template to
    > be able to use this for a vector of any value type. So I would like
    > to know, how many times would a particular template be instantiated?
    > Would it also be one in accordance with the ODR or something?


    There will be one instance for each type.

    --
    Ian Collins
    Ian Collins, May 28, 2013
    #2
    1. Advertising

  3. Shriramana Sharma

    Öö Tiib Guest

    Re: How many times will a template function be instantiated for thesame parameter?

    On Tuesday, 28 May 2013 06:41:25 UTC+3, Shriramana Sharma wrote:
    > Hello. I have this global operator I'm defining in my app for convenience:
    >
    > # include <vector>
    > using namespace std ;


    Notice that you typed those 21 characters for to not type 15 characters
    in rest of your example.

    > template < typename T >
    > vector<T> & operator << ( vector<T> & vec, const T & val ) { vec.push_back(val) ; return vec ; }
    >
    > This is so I can do something like:
    >
    > vector<int> A ;
    > A << 1 << 2 << 3 ;
    >
    > My question is how often will this function be instantiated?


    Once for every type that you use it for. If you use it for int
    but not for float then there will be one for int. Compilers take care
    of following ODR of templates because those are usually defined in
    header files.
    Öö Tiib, May 28, 2013
    #3
  4. Re: How many times will a template function be instantiated for thesame parameter?

    Hello and thanks all for responding.

    On Tuesday, May 28, 2013 10:56:53 AM UTC+5:30, Öö Tiib wrote:
    > > # include <vector>
    > > using namespace std ;

    >
    > Notice that you typed those 21 characters for to not type 15 characters
    > in rest of your example.


    Are you referring to using namespace std ? Well I normally use std:: in short examples but this is actually an except from a bigger program which usesother std:: items so I left it as it is.

    Thanks again.
    Shriramana Sharma, May 28, 2013
    #4
  5. Shriramana Sharma

    SG Guest

    Re: How many times will a template function be instantiated for thesame parameter?

    On May 28, 5:41 am, Shriramana Sharma wrote:
    >
    > # include <vector>
    > using namespace std;
    > template <typename T>
    > vector<T>& operator<<(vector<T> & vec, const T & val)
    > { vec.push_back(val) ; return vec ; }


    Don't put "using namespace std;" in a header file.

    > This is so I can do something like:
    >
    > vector<int> A ;
    > A << 1 << 2 << 3 ;
    >
    > My question is how often will this function be instantiated? I mean,
    > if I had written it like:
    >
    > vector<int>& operator<<(vector<int> & vec, const int & val)
    > { vec.push_back(val) ; return vec ; }
    >
    > then there would be only one definition and this function would be
    > called each time I use the operator. However I'm using a template
    > to be able to use this for a vector of any value type. So I would
    > like to know, how many times would a particular template be
    > instantiated? Would it also be one in accordance with the ODR or
    > something?


    The ODR says that you shall only provide one definition of anything
    within the same translation unit and you shall only provide one
    definition within a program (consinsting of possibly multiple
    translation units) for non-inline, non-template functions.

    So, if you put your non-template into a .cpp file and only its
    declaration in a header you'll be fine.

    So, if you put your function template into a header file so that you
    can use it everywhere, you'll be fine.

    In practice it works like this: When you compile a translation unit
    and make use of a function template in there somewhere, the compiler
    will check whether it already instantiated this function template with
    the same template parameters during the compilation of the translation
    unit. If it did not, it will instantiate it. If yes, there is no need.
    This might result in multiple definitions among different translation
    units. You would get a couple of object files where more than one
    contains the same code of a function template instantiation. But it's
    stored with a special flag to make the linker shut up about it and
    just use a single definition and ignore the duplicates. This is just a
    possible implementation, not mandated by the C++ ISO standard.

    Keep in mind that if you make use of a funciton template in some
    translation unit, you better have its definition available in the same
    translation unit (just like for inline functions). Otherwise you'll
    get linker errors about missing definitions because the compiler won't
    remember that it needs some certain instantiation and later go about
    instantiating it when it finds the template's definition someplace
    else.

    Cheers!
    SG
    SG, May 28, 2013
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    656
  2. sfxpete

    Insert same information many times

    sfxpete, Mar 2, 2007, in forum: ASP .Net
    Replies:
    7
    Views:
    326
    sfxpete
    Mar 2, 2007
  3. Daniel Pitts
    Replies:
    0
    Views:
    501
    Daniel Pitts
    Oct 18, 2007
  4. Jack Steven
    Replies:
    2
    Views:
    429
    Chris Rebert
    Mar 9, 2009
  5. Varun Tewari
    Replies:
    2
    Views:
    220
    Tobias Müller
    Jan 27, 2013
Loading...

Share This Page