Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template function in non-template class - Division between H and CPP files

Tags:

c++

templates

I was (and have been for a long time) under the impression that you had to fully define all template functions in your .h files to avoid multiple definition errors that occur due to the template compilation process (non C++11).

I was reading a co-worker's code, and he had a non-template class that had a template function declared in it, and he separated the function declaration from the function definition (declared in H, defined in CPP). It compiles and works fine to my surprise.

Is there a difference between how a template function in a non template class is compiled, and how a function in a template class is compiled? Can someone explain what that difference is or where I might be confused?

like image 946
John Humphreys Avatar asked Jan 24 '12 23:01

John Humphreys


People also ask

Can a non-template class have a template member function?

A non-template class can have template member functions, if required. Notice the syntax. Unlike a member function for a template class, a template member function is just like a free template function but scoped to its containing class.

Can template functions be in CPP?

To simply put, you can create a single function or single class to work with different data types using templates. C++ template is also known as generic functions or classes which is a very powerful feature in C++.

What is the difference between a template class and class template in C++?

An individual class defines how a group of objects can be constructed, while a class template defines how a group of classes can be generated. Note the distinction between the terms class template and template class: Class template. is a template used to generate template classes.

Can a template class inherit from a non-template class?

Deriving from a non-template base class There is no requirement that your base class be a template. It is quite possible to have a template class inherit from a 'normal' class. This mechanism is recommended if your template class has a lot of non-template attributes and operations.


1 Answers

The interesting bit is how and when the template gets instantiated. If the instantiations can be found at link time, the template definition doesn't need to be visible in the header file.

Sometimes, explicit instantiations are cause like this:

  • header :

    struct X { 
        // function template _declaration_
        template <typename T> void test(const T&);
    };
    
  • cpp:

    #include "X.h"
    
    // function template _definition_:
    template <typename T> 
        void X::test(const T&)
    {
    }
    
    // explicit function template _instantiation(s)_:
    template X::test<int>(const int&);    
    template X::test<std::string>(const std::string&);
    

Using this sample, linking will succeed unless uninstantiated definitions of the template are used in other translation units

like image 186
sehe Avatar answered Sep 29 '22 20:09

sehe