Question

I have a question about template classes. For example, take this class

template<class TBase> class CTemplateInherit : public TBase
{
public:

    virtual void DoNonSpecializedWork();
    virtual void DoTemplateWork();
    virtual ~CTemplateInherit();
};

// In header file
template<class TBase>
bool CTemplateInherit<TBase>::DoTemplateWork()
{
    std::wcout << L"CTemplateInherit::DoTemplateWork" << std::endl;
    TBase::DoWork();
    return true;
}

// In CPP file
bool CTemplateInherit::DoNonSpecializedWork()
{
    std::wcout << L"CTemplateInherit::DoNonSpecializedWork" << std::endl;
    return true;
}

Now, I would like to be able to define non-specialized methods in a CPP file with dllexport, and just keep the specializations in the header. Normally, I suppose I could just define member methods as templated, but since I'm inheriting from TBase that isn't possible.

So how would I be able the split this up? I only override maybe 4 methods from TBase, and would like to be able to keep the other 40 or so methods as DLLEXPORT in the DLL, while the specializations are in the header file.

Thank you in advance for your suggestions!

Était-ce utile?

La solution

I think there is one important piece of information missing: Are the non-specialized methods overriding virtual methods of the TBase class? This is the central issue here.

If not, then you can just create another class for the non-specialized methods, and inherit (publicly) from both classes in your CTemplateInherit class. Problem solved.

If, however, the non-specialized methods override virtual methods of the TBase class, then it is only slightly more complicated:

Solution 1) Take all the non-specialized functions and regroup them into one "detail" header. Either as a group of (non-template) free-functions (if it is easy enough to just pass input-outputs as parameters) or as a (non-template) class with the non-specialized data members it requires. Then, you define all those functions (implement them) inside a corresponding cpp file (which you will later compile into the DLL).

Then, in your CTemplateInherit class template, simply forward the non-specialized function calls to the set of "detail" functions. Because templates inline by default, the overhead will be nil. If you needed to regroup the "detail" functions into one class (non-template), then, simply use private inheritance, preventing function-name conflicts. Then, you can access the data members of the "detail" class like any other inherited data member, and you can forward the non-specialized function calls to the "detail" class implementation which you can compile into a DLL (if you have a suitable framework for exporting classes from a DLL (because plain C++ doesn't have a reliable mechanism for that), but your question seems to imply that you do).

The only issue with this solution is the annoying creation of several simple forwarding functions. But IMHO, that is a reasonable price to pay for tucking away an implementation in a DLL (almost any time you want to do that, you end up writing a bunch of wrapper functions).

Solution 2) If there is a set of non-specialized virtual functions from the base class, then that subset is certainly not dependent on the actual type of TBase (I mean, the prototypes of those functions can be formed without it). Then, it means that you can regroup that subset of functions into another base-class from which TBase is expected to be inheriting from. Let's call it TNonSpecialBase. At this point, you can set up the following hierarchy:

class TNonSpecialBase {
  // set of pure-virtual functions that are not special.
};

// this is an example of a class that could act as a TBase:
class TExampleBase : public virtual TNonSpecialBase {
  // set of virtual functions that are special to TExampleBase.
};

class CNonSpecialDerived : public virtual TNonSpecialBase {
  // declaration of non-specialized functions that override the function in TNonSpecialBase.
};

template <typename TBase>
class CTemplateInherit : public TBase, public CNonSpecialDerived {
  // set of virtual functions that are specialized for the template argument TBase.
};

With the above setup, the specialized functions must end up in the header file of CTemplateInherit, but the non-specialized functions, regrouped in CNonSpecialDerived can be defined in a separate cpp file (and compiled into a DLL). The magic trick here is the use of the virtual inheritance to allow for the final class to have a single virtual-table for the base class TNonSpecialBase. In other words, this allows the class CNonSpecialDerived to override the virtual functions in TBase which are inherited from TNonSpecialBase, as long as TBase does not override any of those functions (in which case, the compiler will call it ambiguous). This way the user can deal with a pointer to a TBase object, call any of its virtual functions, which will cause either a dispatch towards the CTemplateInherit implementations (specialized) or the CNonSpecialDerived implementations (presumably, in the DLL).

Autres conseils

I'm not quite sure what you try to accomplish but when using templates you need to make sure that these are instantiated as necessary. When you put the template definition in the header, you can get implicit instantiation from the compiler: Whenever it sees that a function template is used and there is a definition of this template visible, it will instantiate the template. When you put templates somewhere else the compiler won't see the template definition when it is needed and, thus, won't implicitly instantiate it. However, you can instantiate the corresponding function templates yourself, e.g.:

// In CPP file
template <class TBase>
bool CTemplateInherit<TBase>::DoNonSpecializedWork()
{
    std::wcout << L"CTemplateInherit::DoNonSpecializedWork" << std::endl;
    return true;
}

template bool CTemplateInherit<SomeBase>::DoNotSpecializeWork();
template bool CTemplateInherit<SomeOtherBase>::DoNotSpecializeWork();

If you don't want to change hierarchy of your classes.

1) specialize your function for all needed types in .cpp file.

template<>
bool CTemplateInherit<First>::DoNonSpecializedWork()
{
    std::wcout << L"CTemplateInherit::DoNonSpecializedWork" << std::endl;
    return true;
}

template<>
bool CTemplateInherit<Second>::DoNonSpecializedWork()
{
    std::wcout << L"CTemplateInherit::DoNonSpecializedWork" << std::endl;
    return true;
}

template<>
bool CTemplateInherit<Nth>::DoNonSpecializedWork()
{
    std::wcout << L"CTemplateInherit::DoNonSpecializedWork" << std::endl;
    return true;
}

2) Use

template<typename T>
bool CTemplateInherit<T>::DoNonSpecializedWork()
{
    std::wcout << L"CTemplateInherit::DoNonSpecializedWork" << std::endl;
    return true;
}

in .cpp file, but use something like this in header

template class CTemplateInherit<First>;
template class CTemplateInherit<Second>;
template class CTemplateInherit<Nth>;

3) As Dietmar suggest.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top