鉴于以下情况,工作代码。不知道派生类型的静态CRTP类?
#include <iostream>
template<class Detail>
class AbstractLogger
{
public:
static void log(const char* str) {
Detail::log_detailled(str);
}
};
class Logger : public AbstractLogger<Logger>
{
public:
static void log_detailled(const char* str) {
std::cerr << str << std::endl;
}
};
int main(void)
{
AbstractLogger<Logger>::log("main function running!");
return 0;
}
现在,我想把AbstractLogger
到库中,并让用户库定义自己的记录,像Logger
类在这里。这有一个缺点:AbstractLogger<Logger>
不能在库中使用,因为库不能知道Logger
。
注:
- 请为什么不虚函数或问题。另外,我知道“静态虚拟”成员是无效的类似问题。也许,在CRTP中有一个解决方法:)
- C++ 11会很有趣,但是,我需要“通常”的C++。
你是什么意思*我想把'AbstractLogger'放入一个库*。你的意思是你想在不知道实例化类型的情况下在库中使用它吗?或者你想要某种生成的代码在库中?要么...? –
我的意思是第一个。 – Johannes
闻起来像一个沉重的设计缺陷和CRT模式的滥用... –