Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Explicit instantiation of non-template member of template class

14 views
Skip to first unread message

Noah Roberts

unread,
Jan 31, 2011, 12:43:31 PM1/31/11
to
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

unread,
Jan 31, 2011, 1:25:54 PM1/31/11
to
In article <MPG.27b094a11...@news.mcleodusa.net>,
non...@toemailme.com 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.

Johannes Schaub (litb)

unread,
Jan 31, 2011, 1:35:26 PM1/31/11
to
Noah Roberts wrote:

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)

unread,
Jan 31, 2011, 1:37:46 PM1/31/11
to
Noah Roberts wrote:

> In article <MPG.27b094a11...@news.mcleodusa.net>,
> non...@toemailme.com 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.

Noah Roberts

unread,
Feb 2, 2011, 1:59:53 PM2/2/11
to
In article <ii6v6o$f8o$02$1...@news.t-online.com>,
schaub....@googlemail.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

unread,
Feb 2, 2011, 4:24:36 PM2/2/11
to
In article <MPG.27b349853...@news.mcleodusa.net>,
non...@toemailme.com says...

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/

Johannes Schaub (litb)

unread,
Feb 2, 2011, 4:53:04 PM2/2/11
to
Noah Roberts wrote:

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.

0 new messages