Explicit instantiation of non-template member of template class

N

Noah Roberts

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?
 
N

Noah Roberts

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.
 
J

Johannes Schaub (litb)

Noah said:
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() { }
 
J

Johannes Schaub (litb)

Noah said:
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.
 
N

Noah Roberts

I heard you where interested in what testicles feel like in your mouth.
I think I could help you with that.
 
N

Noah Roberts

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top