Wednesday, March 27, 2019

c++ - Variadic Template Functions: No matching function for call, 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:




Error: no matching function for call to 'LOG_ERROR(const char [14],
int&, )' LOG_ERROR("Sum of x+y
= ", z, std::endl);



note: candidate: 'void LOG_ERROR()' inline void LOG_ERROR() {



note: candidate expects 0 arguments, 3 provided




My Code:



#include 

inline void LOG_ERROR() {
std::cout << std::endl;
}

template
void LOG_ERROR(First && first, Rest && ...rest){
std::cout << std::forward(first);
LOG_ERROR(std::forward(rest)...);
}

int main() {
int foo=40;
LOG_ERROR("My foo = ", foo, 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


Answer



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


No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...