我想弄清楚如何将操纵器传递std::endl给函数,然后在函数中使用传入的操纵器.我可以声明这样的函数:
void f(std::ostream&(*pManip)(std::ostream&));
Run Code Online (Sandbox Code Playgroud)
我可以这样称呼它:
f(std::endl);
Run Code Online (Sandbox Code Playgroud)
那一切都很好.我的问题是弄清楚如何在里面使用操纵器f.这不起作用:
void f(std::ostream&(*pManip)(std::ostream&))
{
std::cout << (*pManip)(std::cout); // error
}
Run Code Online (Sandbox Code Playgroud)
无论编译器如何,错误消息都归结为编译器无法确定operator<<要调用的内容.我需要修复哪些内容f才能编译代码?
void f(std::ostream&(*pManip)(std::ostream&))
{
std::cout << "before endl" << (*pManip) << "after endl";
}
Run Code Online (Sandbox Code Playgroud)
要么
void f(std::ostream&(*pManip)(std::ostream&))
{
std::cout << "before endl";
(*pManip)(std::cout);
std::cout << "after endl";
}
Run Code Online (Sandbox Code Playgroud)