可变参数模板函数:没有匹配的调用函数,std :: endl [英] Variadic Template Functions: No matching function for call, std::endl
问题描述
在我的代码中,我使用 可变模板函数 进行记录。但是当我使用 std :: endl
作为参数时,出现以下编译器错误:
In my code, I use variadic template functions for the logging purpose. But when I use std::endl
as parameter, I get the following compiler error:
错误:没有匹配的函数可以调用'LOG_ERROR(const char [14],
int& ;,)'LOG_ERROR( x + y
=,z,std :: endl );
Error: no matching function for call to 'LOG_ERROR(const char [14], int&, )' LOG_ERROR("Sum of x+y = ", z, std::endl);
注意:候选:'void LOG_ERROR()'内联void LOG_ERROR(){
note: candidate: 'void LOG_ERROR()' inline void LOG_ERROR() {
注意:候选人期望有0个参数,提供3个参数
note: candidate expects 0 arguments, 3 provided
我的代码:
#include <iostream>
inline void LOG_ERROR() {
std::cout << std::endl;
}
template<typename First, typename ...Rest>
void LOG_ERROR(First && first, Rest && ...rest){
std::cout << std::forward<First>(first);
LOG_ERROR(std::forward<Rest>(rest)...);
}
int main() {
int foo=40;
LOG_ERROR("My foo = ", foo, std::endl);
}
该代码在 \n下正常工作
,但我很想学习为什么 std :: endl
失败,以及如何解决
The code works fine with "\n"
but I would love to learn why it fails with std::endl
and how I can fix it
推荐答案
std :: endl
不是字符类型或任何其他类型。它是输出流操纵器。它的返回类型是输出流。
std::endl
is not a character type or any other type. It is output stream manipulator. Its return type is output stream.
因此,没有类型转换,就不能传递它。请在此处
So, you can not pass it without typecasting. Please look here
这篇关于可变参数模板函数:没有匹配的调用函数,std :: endl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!