gpt4 book ai didi

c++ - 为什么 volatile 不使用 std::min 编译

转载 作者:IT老高 更新时间:2023-10-28 22:33:08 35 4
gpt4 key购买 nike

为什么下面的代码无法编译(gcc-5.4.0)?

volatile int i{100};
int j{200};
std::cout << std::min(i, j);

我的意思是我看到编译器错误:

error: no matching function for call to ‘min(volatile int&, int&)’

volatile 不只是提示编译器,变量可以从程序外部更改吗?

std::min(int(i), j);

当然有效。但是原创作品不也应该吗?

最佳答案

volatile 是一个 qualifier 就像 const 一样。这不仅仅是对编译器的提示。

std::min 期望这两个参数具有完全相同的类型和限定符。因此,在您的情况下,它会发出诊断信息。

既然允许你引入限定词,你可以尽情地牵手写字

std::min<volatile int>(i, j)

关于c++ - 为什么 volatile 不使用 std::min 编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47531058/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com