Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class template instantiation

I just read the wiki article about CRTP, and I'm a little confused about template instantiation.

According to the wiki,

member function bodies (definitions) are not instantiated until long after their declarations.

I don't quite understand what it means.

Suppose I got a class template:

template <typename T>
class A
{
    public:
        void foo(T t)
        {
            //...
        };
};

When I instantiate the class template A, does it instantiate the member function foo()?

For example:

//in .cpp file
int main()
{
    A<int> a; //question 1
              //class template is instantiated here, isn't it? 
              //What about foo(), is it instantiated too?

    a.foo(10); //question 2
               //according to the quotation, foo() will not be instantiated until it is used.
               //if so, foo() is instantiated right here, not in question 1, right?
}
like image 867
Alcott Avatar asked Nov 21 '11 11:11

Alcott


People also ask

What is the instantiation of the class template?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation.

How do you instantiate an object in a template class?

To instantiate a template class explicitly, follow the template keyword by a declaration (not definition) for the class, with the class identifier followed by the template arguments. template class Array<char>; template class String<19>; When you explicitly instantiate a class, all of its members are also instantiated.

How many times is the template class instantiation?

cpp, there are two instantiations: template class B<int> and B<float>.

How do I instantiate a template function?

To instantiate a template function explicitly, follow the template keyword by a declaration (not definition) for the function, with the function identifier followed by the template arguments. template float twice<float>(float original); Template arguments may be omitted when the compiler can infer them.


2 Answers

You seem to be confusing one thing:

Instantiation happens during compilation, not during runtime. Hence you can't say "on which line" a class template or a function template was instantiated.

That said, you're right about the fact that member function templates aren't instantiated together with class templates.

You could observe it in such a case: You have the following files

  • template.h (defines class A and function A::foo)
  • a.cpp (uses A)
  • b.cpp (uses A and A::foo)

Then during compilation of a.cpp, only A would be instantiated. However, during compilation of b.cpp, both would be instantiated.

Because of this, in case A::foo contained some semantically invalid code for a given set of template parameters, you would get compile errors in b.cpp, but not a.cpp.

I hope that clears things up!

like image 132
Kos Avatar answered Oct 16 '22 05:10

Kos


With class templates, the rule of thumb is that only those members are instantiated which are actually used.

If you want complete instantiation, C++ offers explicit instantiation (however, usually you don't; the fact that not every bit is fully instantiated means that your template class is even more generic as it lowers the requirements on T, note that syntax checking and lookup of non-dependent types (stuff that is not dependent on T) still happens).

You will find a more complete answer here: Template instantiation details of GCC and MS compilers

like image 34
Sebastian Mach Avatar answered Oct 16 '22 05:10

Sebastian Mach