Using templates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sunil Varma

    Using templates

    Hi,

    I'm trying to write template functions and use them.

    Is it possible to declare a template function in a .h file and define
    it in a .cpp file.

    Ex:

    //temp_func.h
    template <class T>void swap(T &left,T &right);

    //temp_func.cpp
    template <class T>void swap(T &left,T &right)
    {
    T temp = left;
    left = right;
    right = temp;
    }

    //main.cpp
    #include <iostream>
    #include "temp_func. h"

    using namespace std;
    main()
    {
    int a = 10, b = 20;
    cout<<"Before swapping: a = "<<a<<", b = "<<b<<endl;
    swap(a,b);
    cout<<"After swapping: a = "<<a<<", b = "<<b<<endl;
    }

    When I built the above code, I got the following error:

    undefined reference to 'void swap<int>(int&, int&)'

    I know that the instantiation of the swap() function is not available
    in temp_func.o.

    But, how can one make the above code build successfully.

    The above code builds properly if I define the swap() function in
    the .h file itself.

    How do the STL algorithms work?

    Are those functions defined in the headers itself.

    Thanks in advance.

    Regards
    Sunil
  • tragomaskhalos

    #2
    Re: Using templates

    On 15 Jan, 11:43, Sunil Varma <sunil....@gmai l.comwrote:
    Hi,
    >
    I'm trying to write template functions and use them.
    >
    [snip]

    See http://www.parashift.com/c++-faq-lit...html#faq-35.12

    hth

    Comment

    • James Kanze

      #3
      Re: Using templates

      On Jan 15, 12:43 pm, Sunil Varma <sunil....@gmai l.comwrote:
      I'm trying to write template functions and use them.
      Is it possible to declare a template function in a .h file and
      define it in a .cpp file.
      Yes, but...
      Ex:
      //temp_func.h
      template <class T>void swap(T &left,T &right);
      You need to declare it extern here. Which is a problem, since a
      lot of compilers aren't very up-to-date, and don't support
      extern.
      //temp_func.cpp
      template <class T>void swap(T &left,T &right)
      {
      T temp = left;
      left = right;
      right = temp;
      }
      This is, obviously, the only reasonable organization. Given
      that compilers don't support extern, howver, what you have to do
      is add an ``#include "temp_func.cpp" '' to the end of your .h
      file (and of course, ensure that the .cpp is exported along with
      the .h file, when you provide a library).

      A common convention is to use still a different naming
      convention for these files. G++, for example, terminates them
      with .tcc (as opposed to .cc and .hh). Typically, they are
      designed to work only when included at the end of the .hh, and
      shouldn't be included anywhere else.

      Another possibility would be to use export when available, using
      conditional compilation. Basically, your header looks something
      like:

      #ifndef TEMP_FUNC_CPP_a ndSomeRandomJun k

      export template< typename T void swap( T& left, T& right ) ;

      #ifdef export
      #include "temp_func. cpp"
      #endif
      #endif

      If the compiler doesn't support export, you invoke it with
      something like -Dexport= or /Dexport=, whatever is necessary to
      define "export" as an empty string.

      [...]
      How do the STL algorithms work?
      Are those functions defined in the headers itself.
      It depends on the implementation---g++ does as I described, it
      puts the implementations in a .tcc file, which is included from
      the .hh.

      --
      James Kanze (GABI Software) email:james.kan ze@gmail.com
      Conseils en informatique orientée objet/
      Beratung in objektorientier ter Datenverarbeitu ng
      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

      Comment

      Working...