c++ - 使用通过模板注入的模板类型
问题描述
这是我第一次尝试应用确实很难掌握的概念。我创建了一个通用记录器类型,它可以在编译时决定日志级别是否足够高以进行输出。这是编译器资源管理器上的问题示例:https ://godbolt.org/z/2u4HhB 。这是记录器:
static const int debug = 1;
static const int info = 2;
static const int warn = 3;
static const int error = 4;
static const int fatal = 5;
template<int level, bool should_log = false>
struct logger_function {
static void log(std::string message) {
// Do nothing in the default case.
}
};
template<int level>
struct logger_function<level, true> {
static void log(std::string message) {
std::cerr << "[" << level << "] : " << message << std::endl;
}
};
template<int minLevel>
struct std_err_logger {
template<int levelValue>
struct level {
static constexpr bool shouldLogResult = levelValue >= minLevel;
typedef logger_function<levelValue, shouldLogResult> log_function;
static void log(std::string message) {
log_function::log(message);
}
};
};
它是这样使用的:std_err_logger<debug>::level<info>::log("Message.");
到目前为止效果很好。当我尝试通过另一个模板注入记录器类型时,问题就开始了——毕竟我将来可能更喜欢记录到文件中。
template<typename logger>
class log_client {
log_client() {
logger::level<fatal>::log("Worked!");
}
};
并将类型注入客户端:
int main() {
log_client<std_err_logger<debug>> log();
return 0;
}
但是强大的 g++ 编译器并不高兴:
src/alsa/alsa_frame_recorder.h: In constructor ‘alsa::log_client<logger>::log_client()’:
src/alsa/alsa_frame_recorder.h:21:31: error: ‘::log’ has not been declared
logger::level<1>::log("Worked!");
^~~
src/alsa/alsa_frame_recorder.h:21:31: note: suggested alternative: ‘long’
logger::level<1>::log("Worked!");
^~~
long
解决方案
问题是您错误地使用了依赖模板。你应该写:
template<typename logger>
class log_client {
log_client() {
logger::template level<fatal>::log("Worked!");
}
};
有关您为什么需要template
在这里的解释,请参阅此问题。
推荐阅读
- node.js - 如何将图像从客户端发送到服务器节点 js 反应
- flutter - 如何在颤动中从在线加载文件
- java - 在 BarGraph MPAndroidChart 中重叠两个条形图
- python - 使用 Python 在图像上绘制从 Unity 获取的坐标
- git - 如何通过删除 git 中的其他更改(即使在远程)来保持 Head 中的更改?
- html - 如何使用 html、css 和 bootstrap 摆脱垂直和水平滚动条
- flutter - setState 与 StreamProvider
- angular - 我必须在哪个文件中将 serviceWorker 注销代码放入角度 ssr?
- javascript - 如何使用 react-native 和 javascript 通过 id 创建组对象?
- .net - Visual Studio 2019:如何为调试器配置默认 CLR?