派生中缺少静态成员变量时强制编译错误 类

Force compilation error on missing static member variable in derived classes

假设以下 class 层次结构:

//file base.h
class IBase
{
public:
    virtual ~IBase() = default;

    //a static member identifying IBase (e.g. "iid.base")
    static const char* const IID;  //initialize in implementation file
//[...]
};//class IBase

class IDerived : public IBase
{
public:
    virtual ~IDerived() = default;

    //a static member identifying IDerived (e.g. "iid.derived")
    static const char* const IID;  //initialize in implementation file
//[...]
};//class IDerived

class IEvenMoreDerived : public IDerived
{
public:
    virtual ~IEvenMoreDerived() = default;

    //missing static const member IID!
//[...]
};//class IEvenMoreDerived

此层次结构中的每个 class 都必须有自己的 static const tChar* const iid,可用于在不实例化对象的情况下识别对象。在 IEvenMoreDerived 中缺少此信息。

现在,在代码的某处,我有一个访问 IID 的函数模板:

//file main.cpp
#include "base.h"

template<typename T>
void queryIID()
{
    std::cout << T::IID << std::endl;
}

int main(int argc, char* argv[])
{
    queryIID<IBase>();             //prints "iid.base"
    queryIID<IDerived>();          //prints "iid.derived"
    queryIID<IEvenMoreDerived>();  //probably prints "iid.derived"
    return 0;
}

我的意图是在使用 queryIID<IEvenMoreDerived>(); 时出现编译时错误,因为 IEvenMoreDerived 没有静态成员 iid。我假设在函数模板 queryIID() 中使用一些模板魔术可能是可能的,但我尝试的一切都没有解决问题。

补充几点:

作为解决方法,您可以在外部结构中委托 Id,例如:

template <typename T>
struct IdName
{
    static const char* const IID;
};

// instead of `const char* const IBase::IID = "IBase";`
// you have to use the following
template <> const char* const IdName<IBase>::IID = "IBase";
// Same for IDerived
template <> const char* const IdName<IDerived>::IID = "IDerived";

尝试使用 IdName<IEvenMoreDerived>::IID 时出现链接错误。

Live example