Sign in to follow this  
fyhuang

Defining static member functions in a separate file?

Recommended Posts

fyhuang    253
Okay, so say I have this (template!) class with several static member functions:
template < class T >
class ClassName
{
public:
    static void staticFunction();
};
And I wanted to implement those functions in a separate file. Right now I'm doing:
template < class T >
void ClassName<T>::staticFunction()
{
    // ...
}
But this yields linker errors. On the other hand, sticking a 'static' in front of the implementation's function causes a compiler error. My question is - is it possible to do this and if so, how would I go about doing it? The class in question is included in every source file in my program, and right now I'm making tweaks to that class, so I want my implementation in a separate file. Thanks in advance!

Share this post


Link to post
Share on other sites
s_p_oneil    443
Are you using this class in a DLL? Templates combined with static members/methods and DLL's don't aren't easy to get working properly. Also, try moving the function bodies into the header file (as you would do with inline methods).

Share this post


Link to post
Share on other sites
fyhuang    253
I know it's possible to implement the functions in the header file, but that completely defeats the purpose because I don't want to recompile every time I change the implementation. Same with the #including the .cpp. The class I'm using is a Singleton class (the Enginuity Singleton class actually) - is there any other way I could design this class not to use templates?

Thanks!

[edit] It's not in a DLL. Eventually it may be though.

Share this post


Link to post
Share on other sites
SiCrane    11839
If you have a limited, known number of template type arguments you can use explicit template insantiation. ex:

template <class T>
void ClassName<T>::staticFunction() {
// ...
}

template class ClassName<int>;
template class ClassName<float>;

Share this post


Link to post
Share on other sites
s_p_oneil    443
Quote:
Original post by fyhuang
The class I'm using is a Singleton class (the Enginuity Singleton class actually) - is there any other way I could design this class not to use templates?

Thanks!

[edit] It's not in a DLL. Eventually it may be though.


I also borrowed the Enginuity Singleton class, and good luck getting it to work in a DLL. Static members in templates have a problem with DLL's:

1) You create your class and put in a DLL called MyCoreDll. Classes A and B are derived from it in MyCoreDll. MyCoreDll is compiled once and doesn't change.

2) You create another DLL called MyDll1, and it contains class C derived from Singleton. Where does the static go? Does it go in MyCoreDll? It can't because MyCoreDll doesn't know about it.

3) You create another DLL called MyDll2, link it to MyDll1, and try to use class C. How does it know where to import the static member from? If the static member isn't exported, it will actually create another copy of it in MyDll2, breaking the singleton concept. If the static member is exported, in theory it would be forced to look in MyCoreDll for it (that's where the CPP is that contains the code that uses it).

Do you see what kinds of problems can be caused here? There is a very specific syntax you can use with Visual Studio .Net to export static templatized members, but I don't remember what it is, I don't think it works with 6.0, and I doubt it handles the problem above very well.

It is possible to create a Singleton class like that without templates. However, the method that gets the pointer won't be able to return the right type of pointer if it's not templatized unless you declare your GetPtr() methods manually or use a macro to create them. Of course, if you're going to use a macro then you may not even need a singleton base class at all.

This is the type of thing that I can't stand about C++. ;-)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this