std :: endl和可变参数模板 [英] std::endl and variadic template
本文介绍了std :: endl和可变参数模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码:
#include <iostream>
void out()
{
}
template<typename T, typename... Args>
void out(T value, Args... args)
{
std::cout << value;
out(args...);
}
int main()
{
out("12345", " ", 5, "\n"); // OK
out(std::endl); // compilation error
return 0;
}
生成错误:
g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -pthread -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
../main.cpp: In function ‘int main()’:
../main.cpp:17:15: error: no matching function for call to ‘out(<unresolved overloaded function type>)’
../main.cpp:17:15: note: candidates are:
../main.cpp:3:6: note: void out()
../main.cpp:3:6: note: candidate expects 0 arguments, 1 provided
../main.cpp:8:6: note: template<class T, class ... Args> void out(T, Args ...)
../main.cpp:8:6: note: template argument deduction/substitution failed:
../main.cpp:17:15: note: couldn't deduce template parameter ‘T’
> std :: endl 。我如何解决这个问题(除了使用\\\
)?
So, everything is OK except std::endl
. How can I fix this (except of using "\n")?
推荐答案
std :: endl
是一个重载函数,(在许多STL实现中,一个模板),编译器没有关于选择什么的信息。
std::endl
is an overloaded function, (in many STL implementations, a template) and the compiler has no info about what to choose from.
转换为 static_cast< std :: ostream&(*)(std :: ostream&)>(std :: endl)
这篇关于std :: endl和可变参数模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文