首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可变模板推导错误

可变模板推导错误
EN

Stack Overflow用户
提问于 2012-09-21 11:15:10
回答 1查看 410关注 0票数 1

我用c++0x的可变模板写了一个简单的日志函数,但是有模板推导错误,有人能帮帮我吗,谢谢!

代码语言:javascript
复制
#ifndef SLOG_H
#define SLOG_H

enum LOGLEVEL{INFO, DEBUG, ERROR};
/* static */ const char* g_loglevel[] = {"INFO", "DEBUG", "ERROR"};

template <class T>
void slog_(const T& v) {
    std::cout << v;
}

template <class T, class... Args>
void slog_(const T& v, const Args&... args) {
    std::cout << v;
    slog_(args...);
}

template <class T, class... Args>
void slog(LOGLEVEL level, const Args&... args) {
    time_t t;
    struct tm tm;
    char buf[32];

    time(&t);
    localtime_r(&t, &tm);

    strftime(buf, sizeof(buf), "%F %T", &tm);
    std::cout << "[" << g_loglevel[level] << "][" << buf << "] ";

    slog_(args...);
}

#endif

在调用函数中:

代码语言:javascript
复制
slog(INFO, "hello, number ", n, ", next is ", n + 1);

然后编译错误:

代码语言:javascript
复制
main.cpp:6:53: error: no matching function for call to 'slog(LOGLEVEL, const char [15], int&, const char [11], int)'
main.cpp:6:53: note: candidate is:
In file included from main.cpp:2:0:
slog.h:19:6: note: template<class T, class ... Args> void slog(LOGLEVEL, const Args& ...)
slog.h:19:6: note:   template argument deduction/substitution failed:
main.cpp:6:53: note:   couldn't deduce template parameter 'T'

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-21 11:21:14

slogtemplate <class T, class... Args>更改为template <class... Args>。正如错误消息所说:couldn't deduce template parameter 'T',因为您从未将其用作函数参数。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12523754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档