Quantcast
Channel: Intel® C++-Compiler
Viewing all articles
Browse latest Browse all 1665

Compilier bug? const std::string is writeable

$
0
0

Hi all,

this piece of code is rejected by gcc, however icpc on Linux (tested: v14 and 15)

  • does not complain about using a non-const-iterator / changing a const string,
  • and changes the content of the string passed as const, not only in the copy inside cv2up, but also in main.
#include <algorithm>
#include <string>
#include <iostream>

using namespace std;

void cv2up(const std::string typ) {
      // convert to upper case
      std::transform(typ.begin(), typ.end(),typ.begin(), ::toupper);
      cout << typ << endl;
}

int main() {
  string typ("abc");
  cout << typ << endl;
  cv2up(typ);
  cout << typ << endl;
  return 0;
}

 
$ icpc conststr.cpp && ./a.out
abc
ABC
ABC

Regards,

    Christopher


Viewing all articles
Browse latest Browse all 1665

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>