How to prevent a template class from being derived more than once?

前端 未结 4 1939
礼貌的吻别
礼貌的吻别 2021-02-05 07:47

I have the following template class:

template
class T : public I
{
    // ...
};

This template class need to be derived once (an

4条回答
  •  -上瘾入骨i
    2021-02-05 08:37

    The only answer I can envision is to have a registry of instantiations and their derivatives. You could then devise a metafunction that searches the registry. You pass it the base and the derived and if you're not deriving with the registered type it returns void type or something so it causes a compiler error. Based on your requirements that the base has no knowledge of the derived this is the only remotely possible answer I can think of.

    Your declarations would then look something like:

    struct A : search_registry, A>::type { ... };
    

    I believe you're going to run into many issues that will be tough to solve even here--good luck!

    And remember, template metaprograms are purely functional. You can't "add" to the registry in any nice way. You're going to need to define it once to hold everything. On the plus side if someone forgets to add to the registry they'll know, or if they use a previously defined one...on the minus, this is ugly as duck.

提交回复
热议问题