为什么 std::ios_base::sync_with_stdio 没有在 libc++ (clang) 中实现?

Suk*_*lay 5 c++ performance clang c++11 libc++

让我们看一下这个代码示例:

#include <iostream>

int main() {
    std::ios_base::sync_with_stdio(false);

    int n;
    std::cin >> n;
    for (int i = 0; i < n; ++i) {
        int buf;
        std::cin >> buf;
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码示例在输入上的性能如下:

10000000
0
1
...
9999999
Run Code Online (Sandbox Code Playgroud)

在我的机器上:

g++-5 -O2 -std=c++11:

./a.out < input.txt  0.86s user 0.07s system 98% cpu 0.942 total
Run Code Online (Sandbox Code Playgroud)

clang-700.0.72 -O2 -std=c++11

./a.out < input.txt  38.69s user 0.21s system 99% cpu 39.248 total
Run Code Online (Sandbox Code Playgroud)

经过一些分析后,我发现 libc++ 根本不会禁用同步。

然后我查看他们的代码,发现了这个: https: //github.com/llvm-mirror/libcxx/blob/6a85e8a355be05b9efa8408f875014e6b47cef3b/src/ios.cpp#L458

所以我的问题是,这是设计使然还是错误?

Mar*_*low 5

sync_with_stdio据我所知,这只是一个咨询功能。

ios.members.static/2 说:

效果:如果在调用之前使用标准流发生了任何输入或输出操作,则效果是实现定义的。否则,使用 false 参数调用时,它允许标准流独立于标准 C 流进行操作。

恕我直言,“允许……独立运作”是关键。

不要求实现实际执行此操作,而是禁止执行此操作,除非已进行此调用。

所以,是的,这是符合行为。