英特尔 C++ 编译器 - const 字符串是可修改的

Intel C++ compiler - const string is modifiable

我编码如下:

#include <string>
#include <iostream>
#include <boost/algorithm/string.hpp>

using namespace std;

string encode(const string& word) { 
    boost::algorithm::to_upper(word);
    return word;
} 

int main() {
    string word = "a";
    string word1 = encode(word);
    cout << word << endl;
}

编译,输出为"A"。即使该函数采用 const 引用,to_upper 也会对其进行修改。 我正在使用 Intel 的 16.0.2 编译器

在其他编译器(如 g++)上,此代码会引发编译错误。

根据 a post on Intel's developer zone,这是英特尔编译器的一个错误,已在版本 16.0.3(更新 3)中修复。

引用 Judith Ward(英特尔)(02/05/2016)

The underlying problem is that our compiler suppressed discretionary errors that come from system headers (like string and stl_algo.h).

We need to make an exception for errors that are actually useful (i.e. indicative of a potential runtime problem) like this one. This was recently already submitted by another user as DPD200380931 and we just fixed it and have confirmed fixes this problem. This fix did not make the code cutoff for 16.0 update 2 but will be in 16.0 update 3.