简体   繁体   English

调用 std::to_chars 的正确方法是什么?

[英]What is the correct way to call std::to_chars?

I am trying to call std::to_chars on a double.我试图在 double 上调用std::to_chars My code doesn't compile on any compiler I have (gcc 10.2, clang 10.0.1).我的代码无法在我拥有的任何编译器(gcc 10.2、clang 10.0.1)上编译。

Minimally reproducible code ( or on godbolt ):最少可重现的代码(或在 Godbolt 上):

#include <charconv>
#include <string>
int main() {
    std::string str;
    str.resize(30);
    auto[ptr, ec] = std::to_chars(str.data(), str.data() + str.size(), double(3.5));
}

I compiled with -std=c++17 .我用-std=c++17编译。 The error I'm getting is:我得到的错误是:

On gcc 10.2:在 gcc 10.2 上:

<source>: In function 'int main()':
<source>:7:83: error: call of overloaded 'to_chars(char*, char*, double)' is ambiguous
    7 |     auto[ptr, ec] = std::to_chars(str.data(), str.data() + str.size(), double(3.5));
      |                                                                                   ^
In file included from <source>:1:
/.../gcc-10.2.0/include/c++/10.2.0/charconv:366:1: note: candidate: 'std::to_chars_result std::to_chars(char*, char*, char, int)'
...

and then it lists all candidates.然后它列出了所有候选人。

On clang 10.0.1, it pretty much just tells me:在 clang 10.0.1 上,它几乎只是告诉我:

<source>:7:21: error: no matching function for call to 'to_chars'
    auto[ptr, ec] = std::to_chars(str.data(), str.data() + str.size(), double(3.5));
                    ^~~~~~~~~~~~~

I tried all the overloads with double that are listed on cppreference's page on std::to_chars , they all give different errors, but in the end nothing works.我尝试了std::to_charscppreference 页面上列出的所有double重载,它们都给出了不同的错误,但最终没有任何效果。

I do not understand why the overload resolution is not clear when it should be clear, or am I doing something fundamentally wrong here?我不明白为什么重载决议在应该清楚的时候不清楚,或者我在这里做错了什么?

I want to add that MSVC does it fine without any errors.我想补充一点,MSVC 做得很好,没有任何错误。

Your use is perfectly fine, and it compiles correctly under MSVC 19. As described in the Microsoft Docs for the <charconv> functions , this requires at least C++17.您的使用非常好,并且可以在 MSVC 19 下正确编译。如<charconv> 函数Microsoft 文档中所述,这至少需要 C++17。

As you can see for yourself in The GNU C++ Library Manual, Chapter 1. Status, C++ 2017 , the last update regarding P0067R5 (Elementary string conversions, revision 5) was on GCC 8.1, with the comment "only integral types supported".正如您在The GNU C++ Library Manual, Chapter 1. Status, C++ 2017中所看到的,关于 P0067R5(基本字符串转换,修订版 5)的最后一次更新是在 GCC 8.1 上,注释为“仅支持整数类型”。

Similarly, the libc++ C++17 Status , defines P0067R5 as "Partially done".同样, libc++ C++17 Status将 P0067R5 定义为“部分完成”。

If you need more evidence that this is a problem with your C++ implementation, look at the include file for your GCC/Clang installation and you will see that it doesn't define the floating-point overloads (they aren't defined for me, with GCC 10.2.1).如果您需要更多证据证明这是您的 C++ 实现的问题,请查看 GCC/Clang 安装的包含文件,您将看到它没有定义浮点重载(它们不是为我定义的,与海湾合作委员会 10.2.1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM