1

我正在尝试做:

fmt::print(fg(fmt::color::red), "A critical error has occured. consult the logs and fix the issue! {0}", std::endl);

这会导致错误消息:Error C2661 'fmt::v7::print': no overloaded function takes 3 arguments

查看此处的官方文档显示fmt::print为:

 template <typename S, typename... Args>
void fmt::print(const text_style &ts, const S &format_str, const Args&... args)

这表明参数的数量不应该是一个问题,事实上,它不是。如果我用std::endl随机的东西1代替,它编译和构建就好了!这里有什么问题?

4

2 回答 2

4

std::endl是一个模板,但在这种情况下无法确定模板参数,您必须明确指定它们。例如

fmt::print(fg(fmt::color::red), 
           "A critical error has occured. consult the logs and fix the issue! {0}", 
           std::endl<char, std::char_traits<char>>);
//                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
于 2021-04-10T08:21:36.790 回答
3

该错误消息在技术上是不正确的,因为有一个fmt::print需要 3 个参数的重载。但是,即使您能够通过std::endl,也没有任何意义,因为刷新将应用于中间缓冲区,而不是在写入stdout. 您应该使用\n并调用fflush

fmt::print(fg(fmt::color::red),
           "A critical error has occured. consult the logs and fix the issue!\n");
fflush(stdout);

请注意,显式传递模板参数不起作用 - 你只会得到一个不同的错误:https ://godbolt.org/z/T3GYqdchb 。

于 2021-04-11T14:47:57.010 回答