Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3600706
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:31:45+00:00 2026-05-18T20:31:45+00:00

I’m trying to use C++ Template ‘mixins’ to create some new VCL components with

  • 0

I’m trying to use C++ Template ‘mixins’ to create some new VCL components with shared additional functionality. Example…

template <class T> class Mixin : public T
{
private:
  typedef T inherited;

// ...additional methods

public:
  Mixin(TComponent *owner) : inherited(owner)
  {
  // .. do stuff here
  };
};

Used like this:

class MyLabel : public Mixin<TLabel>
{
  ....
}

class MyEdit : public Mixin<TEdit>
{
  ....
}

Now, everything compiles fine, and the mixin stuff seems to work – until I try and save the component to a stream using TStream->WriteComponent, where the inherited properties (eg TLabel.Width/Height/etc.) don’t get written. This is even with a ‘null’ mixin like the one shown above.

My code works fine when just deriving classes directly from TForm, TEdit, etc – and the class is correctly registered with the streaming system.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:31:46+00:00Added an answer on May 18, 2026 at 8:31 pm

    The quick/simple answer is: no; when dealing with a template, the compiler won’t generate the proper descriptors to make streaming working. However, since this has come up before, I peeked under the cover to find out what’s missing. And what I found is that it’s almost there. So here’s a little more information.

    Upfront the compiler will never treat a template-based type as a Delphi. For example, do something like this:

    void testing()
    {
      __classid(Mixin<Stdctrls::TLabel>); // Error Here
    }
    

    … and you’ll see the error

    “Error E2242 test.cpp 53: __classid requires Delphi style class type (i.e. class marked __declspec(delphiclass) or derived from System::TObject) in function testing()”

    This basically says the compiler does not consider this type/class as compatible with Delphi-classes [i.e. those that derive from TObject]. Internally there’s just a flag on the symbol that says whether the type is delphi-compatible or not. And I noticed that I could trick the compiler into marking the type as delphi-style if I forced it to walk up the hierarchy.. which is something it has to do if I create an instance of the object. So, with this hack the error goes away:

    void testing()
    {
      typedef Mixin<Stdctrls::TLabel> __ttype;
      std::auto_ptr<__ttype> c2(new __ttype(0));
      __classid(Mixin<Stdctrls::TLabel>); // No more errors here
    }
    

    But much nicer was actually to use the __declspec(delphiclass) directly on the template, as in:

    template <class T> 
    class __declspec(delphiclass) Mixin : public T {
    private:
      int i;
      typedef T inherited;
    public:
      __fastcall Mixin(TComponent *owner) : inherited(owner) {};
    };
    

    So now that the compiler treats the type as a delphi-style class without hacks, I peeked a little more and found the issue you’re probably running into: Delphi classes have the TTypeData.PropCount field – http://docwiki.embarcadero.com/VCL/en/TypInfo.TTypeData – which is a sum of the class’ properties, including those of its base classes. Due to the way the various pieces of information are computed, the compiler writes out a ‘0’ for that field when a template is involved:(

    You can see this by printing out the PropCount, as in:

    #include <Stdctrls.hpp>
    #include <cstdio>
    #include <memory>
    #include <utilcls.h>
    
    class TCppComp : public Classes::TComponent {
      int i;
    public:
      __fastcall TCppComp(TComponent* owner): Classes::TComponent(owner) {};
    __published:
      __property int AAAA = {read=i, write=i};
    };
    
    template <class T> 
    class __declspec(delphiclass) Mixin : public T {
    private:
      int i;
      typedef T inherited;
    public:
      __fastcall Mixin(TComponent *owner) : inherited(owner) {};
    };
    
    typedef Mixin<TCppComp> TMixinComp;
    
    void showProps(TClass meta) {
      PTypeInfo pInfo = PTypeInfo(meta->ClassInfo());
      int Count = GetPropList(pInfo, tkAny, NULL);
      TAPtr<PPropInfo> List(new PPropInfo[Count]);
      std::printf("Class: %s - Total Props:%d\n", 
                       AnsiString(pInfo->Name).c_str(), Count);  
      GetPropList(pInfo, tkAny, *(reinterpret_cast<PPropList*>(&List)));
      for (int i = 0; i < Count; i++) {
        AnsiString propName(List[i]->Name);
        std::printf("\t%s\n", propName.c_str());
      }
    }
    
    void test() {
      showProps(__classid(TCppComp));
      showProps(__classid(TMixinComp));
    }
    
    int main() {
      test();
      return 0;
    }
    

    When run the above prints:

      Class: TCppComp - Total Props:3
        AAAA
        Name
        Tag
      Class: @%Mixin$8TCppComp% - Total Props:0
    

    IOW, Mixin shows up with ‘0’ published properties while its base type has 3:(

    I suspect the streaming system relies on this count and that’s why inherited properties are not being written out in your setup.

    I considered tweaking the generated descriptors at runtime but since we write them to _TEXT it’s bound to trigger DEP.

    I’ll look at the logic that computes the PropCount to see if there’s some way to get it to compute the correct number. If time allows, please do open a QC for this: now that I’ve peek underneath, I believe it would not require much effort to get this working as expected.

    Cheers,

    Bruneau

    PS: In my sample I even had the Mixin publish a property and the compiler generated the correct descriptor for that property; however, the total count was still zero.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.