意外的std::wstring::resize_and_overwrite结果

huangapple go评论55阅读模式
英文:

Unexpected result from std::wstring::resize_and_overwrite

问题

这个程序的输出似乎与您在cppreference.com上引用的文档不符。根据文档,resize_and_overwrite应该评估std::move(op)(p, count),并且i和c的值应该始终相同。但是,根据您提供的输出,某些迭代中i和c的值不同。这可能是由于一些未定义的行为导致的。

您的代码在循环中调用resize_and_overwrite函数,并且似乎使用了lambda函数来执行某些操作。问题可能出现在lambda函数内部,可能与lambda函数的捕获列表或执行逻辑有关。为了解决这个问题,您可能需要仔细检查lambda函数的实现,确保它按预期工作并正确处理传递给它的参数。

另外,确保您的编译器和编译标志与您在GodBolt上看到的输出相同,因为不同的编译器或编译标志可能会导致不同的行为。

最后,如果问题仍然存在,您可能需要查看C++标准库的实现,以了解resize_and_overwrite函数的确切行为,以确定是否存在任何特定于库实现的问题。

英文:

this program

#include <iostream>
#include <string>


int main()
{
    std::wstring ws;
    for(unsigned i=1;i<64;++i){
		std::cout<<i<<' ';
		ws.resize_and_overwrite(i,[](wchar_t*s,size_t c){std::cout<<c<<'\n';return 0;});
	}

    return 0;
}

outputs

1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 14
9 9
10 10
11 11
12 12
13 13
14 14
15 28
16 16
17 17
18 18
19 19
20 20
21 21
22 22
23 23
24 24
25 25
26 26
27 27
28 28
29 56
30 30
31 31
32 32
33 33
34 34
35 35
36 36
37 37
38 38
39 39
40 40
41 41
42 42
43 43
44 44
45 45
46 46
47 47
48 48
49 49
50 50
51 51
52 52
53 53
54 54
55 55
56 56
57 112
58 58
59 59
60 60
61 61
62 62
63 63

However, according to the doc https://en.cppreference.com/w/cpp/string/basic_string/resize_and_overwrite,
2. Evaluates std::move(op)(p, count).

the values of i and c should always be the same. What is wrong with the code, did I miss something? I guess there must be some undefined behavior at work. The output in https://godbolt.org/ is not the same as that of my PC, but i and c are still different in some iterations.

答案1

得分: 7

您的程序具有明确定义的行为,并且根据当前的C++23草案应始终在每一行上产生相等的值。

您看到的行为是由于libstdc++不符合当前的C++23草案。关于这个问题已经存在一个错误报告 这里

正如错误报告中所述,意图(在我看来似乎是合理的)可能是让用户调用的函数至少有时提供有关分配的实际大小的信息,以便它可以潜在地使用已经分配的额外空间。然而,这不符合标准草案。我不知道Jonathan Wakely是否打算使libstdc++符合标准,还是提议修改标准。我在https://cplusplus.github.io/LWG/lwg-index.html上没有找到任何匹配的开放LWG问题。

英文:

Your program has well-defined behavior and should always produce equal values on each line according to the current C++23 draft.

The behavior you see is due to libstdc++ being non-conforming to the current C++23 draft. A bug report for this already exists here.

As stated in the bug report, the intention (which seems reasonable to me) might have been that the user-callable is (at least sometimes) given information about the actual size of the allocation that way, so that it can potentially use that extra space that is already allocated. However, this is not conforming to the standard draft. I don't know whether Jonathan Wakely intents to make libstdc++ conforming or instead to propose adapting the standard. I didn't find any matching open LWG issue at https://cplusplus.github.io/LWG/lwg-index.html.

huangapple
  • 本文由 发表于 2023年5月28日 20:32:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定