Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / February 2008

Tip: Looking for answers? Try searching our database.

Template in cpp file?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob Altman - 27 Feb 2008 23:27 GMT
Hi all,

I have a project that contains a templated routine.  This same project contains
code that tries to construct an instance of that templated routine.  I have the
following bits and pieces:

Something.h
 // Just the signature, not the implementation
 template <class T> void MyTemplatedRoutine(T arg);

MyTemplatedRoutine.cpp
 // Implementation
 template <class T> void MyTemplatedRoutine(T arg)
 { <implementation> }

X.cpp
 // Try to use it
 #include "Something.h"
 int x = 5;
 a = MyTemplatedRoutine(x);

The linker complains that it can't find void __cdecl
MyTemplatedRoutine<int>(int).  I need to implement the templated routine in a
code file rather than in the header file because it contains references to
static module-level data declared in the code file.  What do I need to do to
make the linker happy?

TIA - Bob
Mark Salsbery [MVP] - 28 Feb 2008 00:05 GMT
> Hi all,
>
[quoted text clipped - 16 lines]
>  int x = 5;
>  a = MyTemplatedRoutine(x);

Your code as shown builds for me fine.  I'm suspicious about the assignment
to "a" by a function that returns void...

Mark

Signature

Mark Salsbery
Microsoft MVP - Visual C++

> The linker complains that it can't find void __cdecl
> MyTemplatedRoutine<int>(int).  I need to implement the templated routine
[quoted text clipped - 3 lines]
>
> TIA - Bob
Bob Altman - 28 Feb 2008 01:30 GMT
> Your code as shown builds for me fine.  I'm suspicious about the assignment to
> "a" by a function that returns void...

Whoops... the assignment to "a" is a typo.  In any event, if I have all of the
plumbing correct then this should work?  I know that exporting templated
functions from a DLL is a pain in the you-know-what, so I thought that maybe I
was running into a variant of that here.  But since everything is in the same
project I would expect that C++ is smart enough to wire it all up correctly even
if the template implementation is in a code file and not directly incorporated
in the referencing code via a header file.

I'll stare at my code some more to see if I can figure it out.  If I can't then
I'll try to simplify it to something as simple as my original posting and I'll
follow up with a new and improved post in this NG.
Mark Salsbery [MVP] - 28 Feb 2008 02:10 GMT
> Hi all,
>
[quoted text clipped - 12 lines]
>
> X.cpp

Argh - I just noticed the 3rd x,cpp file.  That won't work since at that
point, there's not enough info to create an implemented instance of the
template<int> function.

Mark

Signature

Mark Salsbery
Microsoft MVP - Visual C++

>  // Try to use it
>  #include "Something.h"
[quoted text clipped - 8 lines]
>
> TIA - Bob
Ben Voigt [C++ MVP] - 28 Feb 2008 15:47 GMT
> Hi all,
>
[quoted text clipped - 22 lines]
> contains references to static module-level data declared in the code
> file.  What do I need to do to make the linker happy?

Explicitly instantiate the different versions of the template that you need.

Something like

void MyTemplatedRoutine<int>(int arg);

ought to do the trick, or you might need to take its address as in:

static void forceInstantiation()
{
   { void (*force)(int) = &MyTemplatedRoutine<int>; }
   { void (*force)(double) = &MyTemplatedRoutine<double>; }
   // won't this be prettier with C++0x and type inference!
   ...
}

> TIA - Bob

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.