我正在尝试编写一个通用函数来记录一些用于调试的东西,我想这样称之为:
Log("auo", 34); //writes: auo34
Point point;
point.X = 10;
point.Y = 15;
Log(35, point, 10); //writes: 35{10, 15}10
但是,我遇到了参数打包和解包的各种问题,我似乎无法掌握它。以下是完整代码:
struct Point {
long X, Y;
}
std::ofstream debugStream;
template<typename ...Rest>
void Log(Point first, Rest... params) { //specialised for Point
if (!debugStream.is_open())
debugStream.open("bla.log", ios::out | ios::app);
debugStream << "{" << first.X << ", " << first.Y << "}";
Log(params...);
}
template<typename First, typename ...Rest>
void Log(First first, Rest... params) { //generic
if (!debugStream.is_open())
debugStream.open("bla.log", ios::out | ios::app);
debugStream << first;
Log(params...);
}
我该如何修复这些功能?
答案 0 :(得分:4)
采用以下简化版本:
IsDoubleTapEnabled
当void print() {}
template<typename First, typename... Rest>
void print(const First& first, const Rest&... rest)
{
std::cout << first;
print(rest...);
}
发出没有参数的sizeof...(Rest) == 0
时,会发出需要基本情况超载的情况。