# Self Referencing Template as a Base Type

This topic is 796 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to setup something that I don't think works, but I want to know for sure.  I am basically trying to setup a base mixin class, that can use a struct from the derived class.

template <class CLASS>
class BaseMixin
{
std::map<string, CLASS::Data> dataMap;

// Do stuff with the map of data;
};

class Derived
: public BaseMixin<Derived>
{
?public:
struct Data
{
};
};


So far, I don't think this is possible because when deriving from BaseMixin, the Data structure inside Derived isn't yet known, is this correct?  Basically I want a way of dropping in a mixin for certain classes to handle the management of the data, but I want the derived class to actually define the data.  As is, this gives a compiler error that Data is not a member of Derived.  If I try to just use Derived::Data as the template parameter (and change BaseMixin to just use that as is), I get an incomplete type is not allowed compiler error.

So can this be done?

Edited by xycsoscyx

##### Share on other sites
Yes, this is called the curiously recurring template pattern. I could be wrong, but you may need the typename keyword:

std::map<string, typename CLASS::Data> dataMap;

Edited by fastcall22

##### Share on other sites

First, thanks for the name, I knew it was called something and was failing to remember/find what it was.

Second, sadly, this doesn't seem to work for a struct inside the derived class.  It works for (almost?) everything else, member variables, functions, etc, but MSVC won't let me access a inner struct from the templatized class.

[EDIT] Third, I was able to figure something out:

template <class CLASS>
class BaseMixin
{
struct Data : public CLASS::Data
{
};

std::map<string, Data> dataMap;

// Do stuff with the map of data;
};

class Derived
: public BaseMixin<Derived>
{
public:
struct Data
{
};
};


It lets me do what I want, and I can still get the Data inside Derived and use it as expected.  I'm actually not sure why this works compared to my original post, anyone know?  I don't mind using it since it makes sense to me, it just doesn't make sense why I can use the type this way, but not how I originally tried.

It's even more funny because Visual Studio highlights some of the code as errors in Derived when using Data, but it compiles without error and runs as expected.

Edited by xycsoscyx

##### Share on other sites

Second, sadly, this doesn't seem to work for a struct inside the derived class.

Did you make the inner class public?  You didn't in your example.  It would have to be public.

##### Share on other sites

Good point, I ammended my examples to add the public, but my actual implementation already did that.  In my second (working) example, making Data in the Derived class private does actually result in an error about accessing the private struct.  The second example was working with public though, so making it private makes sense that I would get the error.

The part about dependencies in the template would make sense, especially "In resolving dependent names, names from the following sources are considered: Declarations that are visible at the point of definition of the template."  Since the Data struct isn't visible when I inherit from the mixin template, it can't resolve the dependent type.  That wouldn't explain why my second method works though, as that should still be subject to the same restrictions on resolving the dependency?

Edited by xycsoscyx

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633674
• Total Posts
3013277
×