使用原始字符串文字的 C++ 格式

问题描述 投票:0回答:1

您可以将 C++20 format() 与原始字符串文字一起使用吗? gcc 14 返回错误消息。

#include <format>
#include <stdio.h>
#include <string>
using namespace std;

int main(){
    int id=6, sigma_edge_distance=9, width=6, height=9, total=6;
    string buffer = format(R"({"id":{}, "sigma_edge_distance":{}, "width":{}, "height":{}, "total":{}})",
                id, sigma_edge_distance, width, height, total);
    printf("%s", buffer.c_str());
    return 0;
}

错误:

prog.cc: In function 'int main()':
prog.cc:9:27: error: call to consteval function 'std::basic_format_string<char, int&, int&, int&, int&, int&>("{\"id\":{}, \"sigma_edge_distance\":{}, \"width\":{}, \"height\":{}, \"total\":{}}")' is not a constant expression
    9 |     string buffer = format(R"({"id":{}, "sigma_edge_distance":{}, "width":{}, "height":{}, "total":{}})",
      |                     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   10 |                                 id, sigma_edge_distance, width, height, total);
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from prog.cc:1:
prog.cc:9:27:   in 'constexpr' expansion of 'std::basic_format_string<char, int&, int&, int&, int&, int&>("{\"id\":{}, \"sigma_edge_distance\":{}, \"width\":{}, \"height\":{}, \"total\":{}}")'
/opt/wandbox/gcc-head/include/c++/14.0.1/format:4175:19:   in 'constexpr' expansion of '__scanner.std::__format::_Checking_scanner<char, int, int, int, int, int>::std::__format::_Scanner<char>.std::__format::_Scanner<char>::_M_scan()'
/opt/wandbox/gcc-head/include/c++/14.0.1/format:3917:30:   in 'constexpr' expansion of '((std::__format::_Scanner<char>*)this)->std::__format::_Scanner<char>::_M_on_replacement_field()'
/opt/wandbox/gcc-head/include/c++/14.0.1/format:3960:58: error: call to non-'constexpr' function 'void std::__format::__invalid_arg_id_in_format_string()'
 3960 |               __format::__invalid_arg_id_in_format_string();
      |               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/opt/wandbox/gcc-head/include/c++/14.0.1/format:218:3: note: 'void std::__format::__invalid_arg_id_in_format_string()' declared here
  218 |   __invalid_arg_id_in_format_string()
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
c++ string format c++23
1个回答
1
投票

您必须使用双大括号,如

{{ .. }}
才能在格式字符串内使用大括号。

#include <format>
#include <cstdio>
#include <string>
using namespace std;

int main(){
    int id=6, sigma_edge_distance=9, width=6, height=9, total=6;
    string buffer = format(R"({{ "id": {}, "sigma_edge_distance": {}, "width": {}, "height": {}, "total": {} }})",
                id, sigma_edge_distance, width, height, total);
    printf("%s", buffer.c_str());
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.