template classes vs instantiation?

Hi guys I am reading this topic

and in the diagram it shows a template class in blue
and below a instantiation in green

just wondering what is the difference between the two?

basic_istream is a template class yet istream is an instantiation? I thought istream isjust a child class of basic_istream?

for example cout,cin,cerr are objects but what is istream? a class instantiation is an object right?

could someone give me an example in simple code the difference?

thanks
Last edited on
An instance of a class is an object right?

Yes, but...

1
2
template <typename T> 
class A {};

A is not a class, but a class template. An instance of a class template is a class.

In this case, basic_istream is a class template, and istream is another name for an instance of the class template basic_istream named basic_istream<char>. Finally, cin is an object, an instance of the class istream.

1
2
3
4
5
6
7
8
9
10
// class template
template <typename T>
struct basic_istream : public basic_ios<T>
{ /* ... */ };

// class
using istream = basic_istream<char>;
 
// object
istream cin;  // not actually this easy 
Last edited on
ah ok that makes perfect sense,is it possible to create an instance of a class template without creating an object?

for example

1
2
3

vector<char> name;


an template class instance will be made for vector of vector<char> but an object will also be made called name.
typedef vector<char> name;
this is equivalent to
using name = vector<char>;
@poteto Yes, but it's better, IMO.

The latter (the alias syntax) can also declare template names, and I don't have a problem getting the order (i.e., typedef original_type new_name) mixed up.
Last edited on
@OP
Yes - you can explicitly instantiate a template.

I had to edit this post because I made a mistake. We can't explicitly instantiate std::vector<char>, because it's forbidden to instantiate templates from the standard namespace unless the declaration depends on at least one user-defined type. For example, std::vector<my_type> is okay.

You can instantiate it by doing this - just once:
template class std::vector<my_type>;
And the corresponding declaration looks like this:
extern template class std::vector<my_type>;

Leading to code like this:
definition.cpp
1
2
3
4
#include <vector>
struct my_type {};
// declaration and definition
template class std::vector<my_type>;


use.cpp
1
2
3
4
5
6
7
8
9
10
#include <vector>
struct my_type {};
// declaration, but not definition
extern template class std::vector<my_type>;

int main()
{
  // implicit instantiation is skipped: std::vector<my_type> is declared already.
  std::vector<my_type> obj;  
}

Last edited on
thanks mbozzi

just a question,

what is the extern keyword used for in the case above?

and why would you need to have two definitions of template class std::vector<my_type> one in definition.cpp and one in use.cpp?

thanks
extern template class declares (but doesn't define) an explicit instantiation. But just template class both declares and defines an explicit instantiation.

The former is just a forward declaration. It says "this explicit instantiation is defined elsewhere" (e.g., in definition.cpp). This causes implicit instantiation of the declared class to be skipped, and makes the linker look for the missing code elsewhere.

You'd only declare an explicit instantiation to reduce duplicate work on the compiler's part, to speed up compilation.
Topic archived. No new replies allowed.