如何在不过于冗长的情况下获得可变参数模板函数的正确解析
How to get correct resolution of variadic template function without being too verbose
我的问题是关于我在定义和使用可变参数函数时遇到的编译错误。我将在下面给出一个最小的例子。
首先我定义了一个函数来打印一个对象:
(所有代码都是here)
template <typename T>
void print(T t){
std::cout << t<<std::endl;
}
接下来,我定义了一个函数,允许您转换对象然后打印它:
template <typename T>
void print(T t,std::function<void(T& t)> transformer){
transformer(t);
std::cout << t << std::endl;
}
接下来,我想定义一个函数,让您可以调用上述两个函数中的任何一个,但会导致打印标签和结果。我可以使用可变函数模板实现这一点:
template <typename... Params>
void printLabeled(std::string label,Params... params){
std::cout << label << ": ";
print(params...);
}
现在我可以尝试使用代码了。我想做的主要事情是调用 printLabeled("answer",1,{[](int& num){num+=1;}});
,但这不起作用。我必须做一些更冗长的事情。有没有比 printLabeled("answer",1,{[](int& num){num+=1;}});
更冗长的函数调用方法?同样,您可以 运行 代码 here。下面是有效和无效的函数调用示例。
int main() {
//prints the number '1', as expected
print(1);
//prints the number the label "answer" and then the number one
printLabeled("answer",1);
//I intended this to increment 1 to 2 and then print 2,
//but I guess the lambda does not get converted to an std::function,
//so this doesn't work
//print(1,[](int& num){num+=1;});
//This does was the above line was intended to do
print(1,{[](int& num){num+=1;}});
//I intended this to be the labeled version of the above line,
//but for some reason, this doesn't work anymore
// printLabeled("answer",1,{[](int& num){num+=1;}});
//This labeled version does work finally,
//but its pretty verbose
printLabeled("answer",1,std::function<void(int&)>([](int& num){num+=1;}));
return 0;
}
#include <iostream>
#include <string>
template <typename T, typename F>
void print(T t, F transformer){
transformer(t);
std::cout << t << std::endl;
}
template <typename T>
void print(T t){
std::cout << t<<std::endl;
}
template <typename... Params>
void printLabeled(std::string label, Params&&... params){
std::cout << label << ": ";
print(std::forward<Params>(params)...);
}
int main() {
//prints the number '1', as expected
print(1);
//prints the number the label "answer" and then the number one
printLabeled("answer",1);
//This does was the above line was intended to do
print(1, [](int& num){num+=1;});
//I intended this to be the labeled version of the above line,
//but for some reason, this doesn't work anymore
printLabeled("answer", 1, [](int& num){num+=1;});
return 0;
}
我的问题是关于我在定义和使用可变参数函数时遇到的编译错误。我将在下面给出一个最小的例子。
首先我定义了一个函数来打印一个对象: (所有代码都是here)
template <typename T>
void print(T t){
std::cout << t<<std::endl;
}
接下来,我定义了一个函数,允许您转换对象然后打印它:
template <typename T>
void print(T t,std::function<void(T& t)> transformer){
transformer(t);
std::cout << t << std::endl;
}
接下来,我想定义一个函数,让您可以调用上述两个函数中的任何一个,但会导致打印标签和结果。我可以使用可变函数模板实现这一点:
template <typename... Params>
void printLabeled(std::string label,Params... params){
std::cout << label << ": ";
print(params...);
}
现在我可以尝试使用代码了。我想做的主要事情是调用 printLabeled("answer",1,{[](int& num){num+=1;}});
,但这不起作用。我必须做一些更冗长的事情。有没有比 printLabeled("answer",1,{[](int& num){num+=1;}});
更冗长的函数调用方法?同样,您可以 运行 代码 here。下面是有效和无效的函数调用示例。
int main() {
//prints the number '1', as expected
print(1);
//prints the number the label "answer" and then the number one
printLabeled("answer",1);
//I intended this to increment 1 to 2 and then print 2,
//but I guess the lambda does not get converted to an std::function,
//so this doesn't work
//print(1,[](int& num){num+=1;});
//This does was the above line was intended to do
print(1,{[](int& num){num+=1;}});
//I intended this to be the labeled version of the above line,
//but for some reason, this doesn't work anymore
// printLabeled("answer",1,{[](int& num){num+=1;}});
//This labeled version does work finally,
//but its pretty verbose
printLabeled("answer",1,std::function<void(int&)>([](int& num){num+=1;}));
return 0;
}
#include <iostream>
#include <string>
template <typename T, typename F>
void print(T t, F transformer){
transformer(t);
std::cout << t << std::endl;
}
template <typename T>
void print(T t){
std::cout << t<<std::endl;
}
template <typename... Params>
void printLabeled(std::string label, Params&&... params){
std::cout << label << ": ";
print(std::forward<Params>(params)...);
}
int main() {
//prints the number '1', as expected
print(1);
//prints the number the label "answer" and then the number one
printLabeled("answer",1);
//This does was the above line was intended to do
print(1, [](int& num){num+=1;});
//I intended this to be the labeled version of the above line,
//but for some reason, this doesn't work anymore
printLabeled("answer", 1, [](int& num){num+=1;});
return 0;
}