Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a template instantiated?

Tags:

It's an exercise from C++ Primer 5th Edition:

Exercise 16.27: For each labeled statement explain what, if any, instantiations happen. If a template is instantiated, explain why; if not, explain why not. P.677

template <typename T> class Stack { };

void f1(Stack<char>);                   // (a)

class Exercise {
    Stack<double> &rsd;                 // (b)
    Stack<int>    si;                   // (c)
};

int main() {
    Stack<char> *sc;                    // (d)
    f1(*sc);                            // (e)
    int iObj = sizeof(Stack< string >); // (f)
}

Below is what I tried:

(a) Stack<char> is instantiated , but no member of it is instantiated.

(b) Stack<double> is instantiated , but no member of it is instantiated.

(c) Stack<int> and its default constructor are instantiated.

(d) (e) totally no idea...

(f) Stack< string > is instantiated , but no member of it is instantiated.

Am I right? Can anyone tell me how this code is instantiated?

like image 771
Yue Wang Avatar asked Feb 06 '14 09:02

Yue Wang


People also ask

When the templates are usually instantiated?

Automatic Instantiation The bodies of template classes and inline (or static) template functions are always instantiated implicitly when their definitions are needed. Member functions of template classes are not instantiated until they are used. Other template items can be instantiated by using explicit instantiation.

Is it necessary to instantiate a template?

Class template instantiationIn order for any code to appear, a template must be instantiated: the template arguments must be provided so that the compiler can generate an actual class (or function, from a function template).

What is instantiation in C++?

In C++, the creation of a new instance of the class is called instantiation. Memory is allocated for that object and the class constructor runs. Programmers can instantiate objects on the heap with a new keyword or on the stack as a variable declaration.

What is the use of template and how its work?

A template or design template is a file that acts as a starting point for a new document. It is used with one or more documents and created with an overall design. For example, in Microsoft Word, you might use a template for making bio-data, resume or format as a business letter.


1 Answers

In your specific case a declaration doesn't mean an instantiation

#include <iostream>
using namespace std;


template <typename T> class Stack {
  typedef typename T::ThisDoesntExist StaticAssert; // T::ThisDoesntExist doesn't exist at all!
};


void f1(Stack<char>); // No instantiation, compiles

class Exercise {
  Stack<double> &rsd; // No instantiation, compiles (references don't need instantiation, are similar to pointers in this)
  
  Stack<int>    si; // Instantiation! Doesn't compile!!
};


int main(){
  
  Stack<char> *sc; // No Instantiation, this compiles successfully since a pointer doesn't need instantiation
  
  f1(*sc); // Instantiation of Stack<char>! Doesn't compile!!

  int iObj = sizeof(Stack< std::string >); // Instantiation of Stack<std::string>, doesn't compile!!
 
}

notice the pointer/reference stuff: they don't require instantiation since no data is actually allocated (a pointer is just a few bytes to contain the address, has no need to have all the data stored.. take a look at the pimpl idiom ).

Only when stuff is allocated then the template has to be completely resolved (and that happens at compile-time, that's why they usually need both declaration and definition.. there's no linking phase yet)

like image 109
Marco A. Avatar answered Oct 24 '22 22:10

Marco A.