Explicit instantiation of non-template member of template class

Discussion in 'C++' started by Noah Roberts, Jan 31, 2011.

  1. Noah Roberts

    Noah Roberts Guest

    I thought I knew how to do this but either I don't or my compiler is a
    POS (which may be true anyway). I have:

    namespace whatnot {

    template < typename T >
    struct bob
    {
    void f(int) { }
    };

    }

    Elsewhere, where I have definitions for a particular object I need to
    use bob on:

    #include <thingy.h>
    namespace whatnot {

    template < >
    void bob<thigy>::f(int);

    }

    in a cpp:
    #include "specialization.h"

    namespace whatnot {

    template < >
    void bob<thingy>::f(int) { thingy::do_it(); }

    }

    }

    When I try this though I get linker errors saying that void bob
    <thingy>::f(int) is missing. Surprisingly, to me anyway, if I put the
    body of my f specialization into the specialization.h file everything
    seems to work ok, and I'm not using the inline keyword.

    I thought I recalled a conversation elsewhere that implied the "template
    < >" was not needed for cases like this so I tried removing it in
    various different ways. No go. Google searches seem to indicate the
    above should work.

    So, I'm probably being stupid, so what is it?

    --
    http://crazycpp.wordpress.com/
    Noah Roberts, Jan 31, 2011
    #1
    1. Advertising

  2. Noah Roberts

    Noah Roberts Guest

    In article <>,
    says...
    >
    > I thought I knew how to do this but either I don't or my compiler is a
    > POS (which may be true anyway). I have:
    >
    > namespace whatnot {
    >
    > template < typename T >
    > struct bob
    > {
    > void f(int) { }
    > };


    Additional tidbit I missed... f is virtual due to inheritance.
    >
    > }
    >
    > Elsewhere, where I have definitions for a particular object I need to
    > use bob on:
    >
    > #include <thingy.h>
    > namespace whatnot {
    >
    > template < >
    > void bob<thigy>::f(int);
    >
    > }
    >
    > in a cpp:
    > #include "specialization.h"
    >
    > namespace whatnot {
    >
    > template < >
    > void bob<thingy>::f(int) { thingy::do_it(); }
    >
    > }
    >
    > }
    >
    > When I try this though I get linker errors saying that void bob
    > <thingy>::f(int) is missing. Surprisingly, to me anyway, if I put the
    > body of my f specialization into the specialization.h file everything
    > seems to work ok, and I'm not using the inline keyword.
    >
    > I thought I recalled a conversation elsewhere that implied the "template
    > < >" was not needed for cases like this so I tried removing it in
    > various different ways. No go. Google searches seem to indicate the
    > above should work.
    >
    > So, I'm probably being stupid, so what is it?




    --
    http://crazycpp.wordpress.com/
    Noah Roberts, Jan 31, 2011
    #2
    1. Advertising

  3. Noah Roberts wrote:

    > I thought I knew how to do this but either I don't or my compiler is a
    > POS (which may be true anyway). I have:
    >
    > namespace whatnot {
    >
    > template < typename T >
    > struct bob
    > {
    > void f(int) { }
    > };
    >
    > }
    >
    > Elsewhere, where I have definitions for a particular object I need to
    > use bob on:
    >
    > #include <thingy.h>
    > namespace whatnot {
    >
    > template < >
    > void bob<thigy>::f(int);
    >
    > }
    >
    > in a cpp:
    > #include "specialization.h"
    >
    > namespace whatnot {
    >
    > template < >
    > void bob<thingy>::f(int) { thingy::do_it(); }
    >
    > }
    >
    > }
    >
    > When I try this though I get linker errors saying that void bob
    > <thingy>::f(int) is missing. Surprisingly, to me anyway, if I put the
    > body of my f specialization into the specialization.h file everything
    > seems to work ok, and I'm not using the inline keyword.
    >
    > I thought I recalled a conversation elsewhere that implied the "template
    > < >" was not needed for cases like this so I tried removing it in
    > various different ways. No go. Google searches seem to indicate the
    > above should work.
    >
    > So, I'm probably being stupid, so what is it?
    >


    The way you presented it, it should work. Of course, I'm assuming that you
    have "specialization.h" included when you call "f(int)", such that the
    specialization is visible.

    You need the "template<>" to introduce a specialization of that member. You
    don't need it when defining a member of a class that does not depend on
    template parameters anymore.

    template<typename T> struct A {
    void f();
    template<typename U> struct B { void f(); };
    };

    template<> struct A<int> { void f(); };

    Given these, you would go like

    template<> void A<long>::f() { }
    template<> template<typename U> A<long>::B<U>::f() { }
    void A<int>::f() { } // !!

    template<> template<typename U> struct A<unsigned>::B {
    void f();
    };

    template<> template<typename U> void A<unsigned>::B<U>::f() { }
    Johannes Schaub (litb), Jan 31, 2011
    #3
  4. Noah Roberts wrote:

    > In article <>,
    > says...
    >>
    >> I thought I knew how to do this but either I don't or my compiler is a
    >> POS (which may be true anyway). I have:
    >>
    >> namespace whatnot {
    >>
    >> template < typename T >
    >> struct bob
    >> {
    >> void f(int) { }
    >> };

    >
    > Additional tidbit I missed... f is virtual due to inheritance.
    >


    Virtual non-pure functions are always instantiated even if not called. So
    you need to always include "specialization.h" even if you don't call "f", in
    each TU and before the point where you cause any bob<thingy> instantiation
    to occur.
    Johannes Schaub (litb), Jan 31, 2011
    #4
  5. Noah Roberts

    Noah Roberts Guest

    In article <ii6v6o$f8o$02$-online.com>,
    says...

    I heard you where interested in what testicles feel like in your mouth.
    I think I could help you with that.
    Noah Roberts, Feb 2, 2011
    #5
  6. Noah Roberts

    Noah Roberts Guest

    In article <>,
    says...
    >
    > In article <ii6v6o$f8o$02$-online.com>,
    > says...
    >
    > I heard you where interested in what testicles feel like in your mouth.
    > I think I could help you with that.


    This was inappropriate and I appologize to the group. I really hate the
    guy, and he knows why, but I shouldn't have brought that here.

    --
    http://crazycpp.wordpress.com/
    Noah Roberts, Feb 2, 2011
    #6
  7. Noah Roberts wrote:

    > In article <>,
    > says...
    >>
    >> In article <ii6v6o$f8o$02$-online.com>,
    >> says...
    >>
    >> I heard you where interested in what testicles feel like in your mouth.
    >> I think I could help you with that.

    >
    > This was inappropriate and I appologize to the group. I really hate the
    > guy, and he knows why, but I shouldn't have brought that here.
    >


    Yes, your "jokes" ( http://stackoverflow.com/users/301883/noah-roberts ) are
    not welcome here. Take them into one of your offensive answers you regularly
    give on that platform.
    Johannes Schaub (litb), Feb 2, 2011
    #7
    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. Fernando Cuenca
    Replies:
    4
    Views:
    2,510
    Gianni Mariani
    Sep 6, 2004
  2. Thomas Maier-Komor
    Replies:
    6
    Views:
    600
    Thomas Maier-Komor
    May 19, 2005
  3. Replies:
    5
    Views:
    475
  4. Replies:
    1
    Views:
    558
    Salt_Peter
    Dec 25, 2006
  5. highegg
    Replies:
    1
    Views:
    328
    Neelesh
    Jun 3, 2009
Loading...

Share This Page