[英]How pass std::endl to a function and use it?
我想弄清楚如何将类似std::endl
的操纵器传递给函数,然后在函数中使用传入的操纵器。 我可以这样声明函数:
void f(std::ostream&(*pManip)(std::ostream&));
我可以这样称呼它:
f(std::endl);
没事的 我的问题是弄清楚如何在f
使用操纵器。 这不起作用:
void f(std::ostream&(*pManip)(std::ostream&))
{
std::cout << (*pManip)(std::cout); // error
}
无论编译器如何,错误消息都归结为编译器无法确定要调用哪个operator<<
。 我需要在f
内部修复什么才能使我的代码编译?
void f(std::ostream&(*pManip)(std::ostream&))
{
std::cout << "before endl" << (*pManip) << "after endl";
}
要么
void f(std::ostream&(*pManip)(std::ostream&))
{
std::cout << "before endl";
(*pManip)(std::cout);
std::cout << "after endl";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.