Is it possible to hide the implementation of a template class?

I have a library "myLibrary" which depends on "library". I want to prevent users of "myLibrary" from knowing this dependency. I'm trying to hide it with no luck, here's an example of what I have now.

#include <game/Object.h>
#include <Blibrary/Component.hpp>   // How can I remove this library header?
                                    // forward declaring it? it a template..

namespace myLibrary {

    template<typename T>
    struct Component: public Object, public Blibrary::Component<T>
    {
    };


    //template<typename T>
    //class Blibrary::Component<T>;    //I Tried something like that..

    //template<typename T>
    //struct Component: public Object
    //{
    //  Blibrary::Component<T> * m_impl;
    //};
}



//I want the user do this when declaring a usermade component:

#include <game/Component.h>   //<-- but without the Blibrary include dependency


class Position: public myLibrary::Component<Position>
{
    float x, y, z;
};

      

+3


source to share


1 answer


Is it possible to hide the implementation of a template class?

No, it is not. The class template must be fully defined in the header files. You can only obfuscate the implementation by using multiple layers of header files and using helper class names and helper function names that are the highest level obfuscations of user-visible classes.



However, as @vsoftco points out in a comment, you can hide it if you only use it for certain types, in which case you can do explicit instantiation, export the template and implement it in .cpp.

+10


source







All Articles