Question

I have a bunch of related indicies in a kind of templated hierarchy, looking something like

template <int level>
struct index{
    index<level - 1> w;
    int x, y;
};

template <> struct index<0> { int x, y; };

template <int level>
struct data;

and a class that's supposed to generate and cache objects indexed by them. I want to use pimpl for this class, and I'm wondering if there's a way to forward the function calls to the implementation class using templates. Something like

class Cache{
    template <int level>
    shared_ptr<data<level>> get_data(const index<level> & index);
};  
Was it helpful?

Solution

In short, no (if I understand your question correctly).

The problem is that at the point of implementation of your forwarder, you need the full definition (and not just a declaration) of your implementation class. If you want to use template member functions for this purpose you need to define them in every compilation unit that uses them, i.e. typically in the same header where you declare them. This means that the implementation class needs to be defined in the same header where you declare your interface class (which would defeat the purpose of pimpling the class in the first place).

(In case your compiler supports the export keyword, you can define template methods separately, so in this case it would work. In practice, Comeau is the only compiler I know that supports export, and it has been removed from C++11 completely).

Compare How can I avoid linker errors with my template functions? in the C++-FAQ-lite.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top