Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Beginner
Jul 11, 2000

Dynamic Templated Objects

Question:
How does the compiler handle dynamically allocated templated objects? As I have read, the compiler generates code for templated objects and functions according to the required data type.

Answer:
A template class specialization (i.e., a template instance) is defined at compile time. Its code is generated on demand. For example:

std::vector  vi; // generate a vector of int

The compiler isn't responsible for dynamic allocation; it's the runtime environment (usually the OS kernel) that handles the allocation request at runtime, when operator new (or new[] for arrays) is invoked. Thus, when programmers allocate a template object, they allocate an object that already exists; the code for the template instance is generated at compile time, before the allocation takes place. In other words, allocating a template instance is just like allocating any other object.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date