Home > Mobile >  In the Curiously Recurring Template Pattern (CRTP), when does the dependent type name become a compl
In the Curiously Recurring Template Pattern (CRTP), when does the dependent type name become a compl

Time:12-23

I want to have classes with a static data member knowing the class's complete size. This is for storing singleton instances, in case you want to know the actual use case of this.

In my naive implementation of this feature, I wanted to use a mixin class to add the special data member to my class. The mixin class would have to know the complete class (in order to know the complete class's size), so I implement it using the Curiously Recurring Template Pattern, a little bit like this:

template<class ObjectType>
class SingletonOf
{
  static inline /* some type same size as ObjectType */ instance_memory;
public:
  void *operator new(std::size_t)
  {
    return &instance_memory;
  }
  void operator delete(void *)
  {
  }
};

class foo : public SingletonOf<foo> // CRTP used here, to let SingletonOf know foo
{
  // foo data members...
  // foo member functions...
};

void bar() {
  foo *p = new foo; // calls SingletonOf<foo>::operator new and returns the instance memory
}

Cute, right? Well, I learned that the following in C 20 is ill-formed (note: in all the code samples below, the class foo and the function bar() do not change. Also I will not keep writing the empty definition of SingletonOf::operator delete, because you can remember that it's there):

template<class ObjectType>
class SingletonOf
{
  static char inline instance_memory[sizeof(ObjectType)]; // syntax error: incomplete type
public:
  void *operator new(std::size_t) { return instance_memory; }
...

Now, we will all agree the reason why that is ill-formed - and I am not complaining, just informing - is that ObjectType is foo, and until the closing brace of foo, foo is an incomplete type. And, obviously, sizeof cannot be called on incomplete types. So, I am fine with that. However, the following using a nested class-template does work - at least according to clang in c 20 mode, I think?

template<class ObjectType>
class SingletonOf
{
  template<class CompleteObjectType>
  struct InstanceMemory
  {
    static char inline instance_memory[sizeof(CompleteObjectType)];
  };
public:
  void *operator new(std::size_t) {
    return InstanceMemory<ObjectType>::instance_memory;
  }
...

Now my question is: why does that work? Or, let's start with the more fundamental question: does that work, actually? As of this writing, just to be clear, I have not verified that bar() actually calls the intended operator new and returns the foo-sized instance memory. Probably, should do that. But I'm busy. What I do know at this time, is that my clang in c 20 mode compiles it. This compilation includes compiling the function bar(), which allows me to be certain it instantiates the template. So that is to back up my contention that the compiler is accepting it. There are no errors or warnings give, just an output object file.

If I am right that this second code is well-formed, then it looks like ObjectType (= foo) in the body of operator new in the second code sample, is considered a complete type. How did that happen?

CodePudding user response:

This isn’t really any different from having InstanceMemory defined in a namespace: until it is instantiated, its template argument need not be complete. This separation works because it removes the presumption that you should be able to use decltype(SingletonOf::instance_memory) immediately after declaring it.

CodePudding user response:

When SingletonOf<ObjectType> is being instantiated, ObjectType is incomplete. That's why you can't get the size of it.

However, the member function bodies of SingletonOf work as if they are placed just after the type. And those functions get instantiated at a point when ObjectType is complete. This is why ObjectType is complete and visible to member functions of SingletonOf<ObjectType>.

Your inner struct InstanceMemory is itself a template. And you instantiate it within a member function of the outer template. Since that member function sees ObjectType as complete, so too does InstanceMemory<ObjectType>.

All you have to do is make sure to instantiate InstanceMemory<ObjectType> at a point where ObjectType is complete.

  •  Tags:  
  • c
  • Related