【问题标题】:Error C2661 'fmt::v7::print': no overloaded function takes 3 arguments错误 C2661 'fmt::v7::print':没有重载函数需要 3 个参数
【发布时间】:2021-07-05 23:42:47
【问题描述】:

我正在努力:

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

查看官方文档herefmt::print 显示为:

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

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

【问题讨论】:

    标签: c++ templates fmt


    【解决方案1】:

    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>>);
    //                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    【讨论】:

    • 技术上正确,归档在#unhelpfullyhelpful
    【解决方案2】:

    错误消息在技术上是不正确的,因为有一个 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

    【讨论】:

      猜你喜欢
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 2017-05-01
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多