template specialization overriding non-specialization?

Discussion in 'C++' started by Joseph Turian, Apr 16, 2006.

  1. I'm posting this question for a friend who lacks USENET access.
    He and I were discussing this question and could not figure out the
    solution.

    Thank you for your help

    Joseph

    ===

    I have a template class with a bunch of methods that are used for most
    of the possible template parameter types. However, for a couple of
    types the implementation needs to be different. The solutions for
    this given on
    http://www.parashift.com/c -faq-lite/templates.html#faq-35.7 don't
    seem to work. If I include the instantiations at the bottom of the
    definitions (.cpp) file, the compiler seems not to notice the
    specialization (in a separate .cpp file). If I include that other
    ..cpp file in the first one, the compiler complains about multiple
    instantiations. Anybody have any suggestions or working examples of
    this? I'm using gcc-4 on red hat linux.

    TIA,
    Joseph Turian, Apr 16, 2006
    #1
    1. Advertising

  2. Joseph Turian wrote:

    > I have a template class with a bunch of methods that are used for most
    > of the possible template parameter types. However, for a couple of
    > types the implementation needs to be different. The solutions for
    > this given on
    > http://www.parashift.com/c -faq-lite/templates.html#faq-35.7 don't
    > seem to work. If I include the instantiations at the bottom of the
    > definitions (.cpp) file, the compiler seems not to notice the
    > specialization (in a separate .cpp file). If I include that other
    > .cpp file in the first one, the compiler complains about multiple
    > instantiations. Anybody have any suggestions or working examples of
    > this? I'm using gcc-4 on red hat linux.


    The reason for your problem is explained in FAQ 35.12:

    http://www.parashift.com/c -faq-lite/templates.html#faq-35.12

    The most straight-forward solution is to put both the template class
    definition and all the specializations in one or more header files - do
    not put any template definitions or specializations in a cpp file. If
    you use the template or any specializations, make sure that cpp file
    includes the appropriate header files prior to the use. Do not (in my
    view, ever) include one cpp file in another - put it in the header
    files.

    If you want anything more specific, you're going to need to post code
    that illustrates your problem. If you do so, keep it short and make
    sure it is compilable (no ellipses or "[my code here"] - so that we can
    copy it and compile it ourselves to see your problem.

    Best regards,

    Tom
    Thomas Tutone, Apr 16, 2006
    #2
    1. Advertising

  3. Joseph Turian

    Guest

    Joseph Turian wrote:

    > Anybody have any suggestions or working examples of
    > this? I'm using gcc-4 on red hat linux.


    As Thomas Tutone says it would help to post a minimal example of what
    you are trying to do. It sounds like you want to specialise a member
    function for one or more members without inlining the function. In the
    header file declare but dont define the function:

    // "test.hpp"
    #ifndef TEST_HPP_INCLUDED
    #define TEST_HPP_INCLUDED
    template <typename T>
    struct S{
    void func();
    };

    #endif
    //--------------------------

    Then in a .cpp file do the definitions inc required specialisations
    Followed by instantiation requests for any templates you need to use

    // "test1.cpp"
    #include "test.hpp"
    #include <iostream>

    template <typename T>
    void S<T>::func(){
    std::cout << "unspecialised\n";
    }

    template <>
    void S<int>::func()
    {
    std::cout << "specialised\n";
    }

    // request instantiations AFTER showing the definitions
    // and any specialisations
    template
    void S<int>::func();
    template
    void S<double>::func();

    //--------------------------------

    // link the object code from test1.cpp in your project

    // check it works...

    // "main.cpp"

    #include "test.hpp"

    int main()
    {
    S<double> s_double;
    s_double.func();

    S<int> s_int;
    s_int.func();
    }

    //---------------------------

    // should output:
    unspecialised
    specialised

    HTH

    regards
    Andy Little
    , Apr 16, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Massimiliano Alberti
    Replies:
    8
    Views:
    4,761
    Massimiliano Alberti
    Feb 7, 2004
  2. er
    Replies:
    7
    Views:
    388
    Ian Collins
    Nov 11, 2007
  3. Bill
    Replies:
    7
    Views:
    397
    Marcel Müller
    Feb 22, 2008
  4. nguillot
    Replies:
    5
    Views:
    530
  5. vj
    Replies:
    1
    Views:
    482
Loading...

Share This Page