Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Explicit instantiation of non-template member of template class

Reply
Thread Tools

Explicit instantiation of non-template member of template class

 
 
Noah Roberts
Guest
Posts: n/a
 
      01-31-2011
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/
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      01-31-2011
In article <(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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/
 
Reply With Quote
 
 
 
 
Johannes Schaub (litb)
Guest
Posts: n/a
 
      01-31-2011
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() { }


 
Reply With Quote
 
Johannes Schaub (litb)
Guest
Posts: n/a
 
      01-31-2011
Noah Roberts wrote:

> In article <(E-Mail Removed)>,
> (E-Mail Removed) 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.

 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      02-02-2011
In article <ii6v6o$f8o$02$(E-Mail Removed)-online.com>,
(E-Mail Removed) says...

I heard you where interested in what testicles feel like in your mouth.
I think I could help you with that.
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      02-02-2011
In article <(E-Mail Removed)>,
(E-Mail Removed) says...
>
> In article <ii6v6o$f8o$02$(E-Mail Removed)-online.com>,
> (E-Mail Removed) 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/
 
Reply With Quote
 
Johannes Schaub (litb)
Guest
Posts: n/a
 
      02-02-2011
Noah Roberts wrote:

> In article <(E-Mail Removed)>,
> (E-Mail Removed) says...
>>
>> In article <ii6v6o$f8o$02$(E-Mail Removed)-online.com>,
>> (E-Mail Removed) 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
explicit instantiation of member class highegg C++ 1 06-03-2009 08:39 AM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
?explicit instantiation member function of class template henkoo@gmail.com C++ 5 12-04-2005 04:06 PM
explicit instantiation of template methods of template classes Thomas Maier-Komor C++ 6 05-19-2005 08:00 AM
Explicit template instantiation from template function doesn't compile? Fernando Cuenca C++ 4 09-06-2004 04:06 PM



Advertisments