Jump to content
  • Advertisement
Sign in to follow this  
xycsoscyx

Self Referencing Template as a Base Type

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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.

 

I found an interesting question about this here: http://stackoverflow.com/questions/17478621/when-instantiating-a-template-should-members-of-its-incomplete-argument-types-b

 

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!