mca*_*ca2 3 c++ visual-studio-2022
在 MSVS 2019 中,我曾经声明我的发行版const。然而在最新的 MSVS2022 预览中,我收到一个错误:
error C3848: expression having type 'const std::uniform_real_distribution<double>' would lose some const-volatile qualifiers in order to call 'double std::uniform_real<_Ty>::operator ()<std::mt19937>(_Engine &)'
1> with
1> [
1> _Ty=double,
1> _Engine=std::mt19937
1> ]
Run Code Online (Sandbox Code Playgroud)
如果我剥掉const一切就又好了。这是 MSVS2022 的怪癖吗?还是2019年一直都是错的?(示例来自https://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution)
error C3848: expression having type 'const std::uniform_real_distribution<double>' would lose some const-volatile qualifiers in order to call 'double std::uniform_real<_Ty>::operator ()<std::mt19937>(_Engine &)'
1> with
1> [
1> _Ty=double,
1> _Engine=std::mt19937
1> ]
Run Code Online (Sandbox Code Playgroud)
use*_*522 10
const std::uniform_real_distribution<>从未保证可用。该标准没有具体说明operator()是std::uniform_real_distribution。const特别是<random>的分布允许具有内部状态,例如,记住从生成器返回的未使用的位,或者在转换算法需要时存储状态。
但是,如果标准库实现添加const非虚拟成员函数不会影响对根据标准规范在没有非虚拟成员函数的情况下格式良好的函数的任何调用,则允许添加该非虚拟成员函数。因此,微软的实现在允许您使用该const版本方面并不不符合标准,即使不能保证按照标准工作。
尽管如此,微软的标准库开发人员还是决定删除const默认情况,以鼓励其他标准库实现的可移植性。他们留下了定义选项_ALLOW_RANDOM_DISTRIBUTION_CONST_OPERATOR(例如在命令行上/D_ALLOW_RANDOM_DISTRIBUTION_CONST_OPERATOR),以便在有人确实需要时恢复旧的不可移植行为。
所有这些都在相应的问题https://github.com/microsoft/STL/issues/1912和拉取请求https://github.com/microsoft/STL/pull/2732中进行了解释和讨论。