Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How many times will a template function be instantiated for the same parameter?

Reply
Thread Tools

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

 
 
Shriramana Sharma
Guest
Posts: n/a
 
      05-28-2013
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?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-28-2013
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
 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      05-28-2013
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.
 
Reply With Quote
 
Shriramana Sharma
Guest
Posts: n/a
 
      05-28-2013
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.
 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      05-28-2013
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to break a bash command into an array consisting of the argumentsin the command? Peng Yu Perl Misc 3 05-13-2013 10:27 AM
Software must support the user's settings for font size but how itworks with swing? Markus Lemcke Java 6 05-01-2013 05:16 PM
How to capture the “same” RGB images for the same scene using EDSDK? azuring@gmail.com Digital Photography 0 04-28-2013 12:08 PM
Considering File Content: Will the NetBeans IDE (deployed version)produce files identical to the javac compiler files clusardi2k@aol.com Java 4 04-26-2013 08:37 PM
How many times is too many times? Jules W MCSA 3 08-30-2005 07:33 PM



Advertisments