Possible Bug in Reference to Pointer to Const

This is with VS 2005

void F(const char *&pChr) {

pChr = "Test";

}

int _tmain(int argc, _TCHAR* argv[]) {

char *pChr1;

const char *pChr2;

F(pChr1); // Fails: cannot convert parameter 1 from 'char *' to 'const char *&'

F((const char *)pChr1); // Fails: cannot convert parameter 1 from 'const char *' to 'const char *&'

F(pChr2); // Succeeds

return 0;

}



Answer this question

Possible Bug in Reference to Pointer to Const

  • VisuallyImpaired

    This used to work in VC++ 6. I've checked my annotated C++ reference and do not see what you say. I would have thought that the cast in the second line would have made the reference possible.
  • KrishManohar

    VC6 let you get away with a lot of things; its relative level of C++ conformance was considered very bad. This caused a lot of code to be non-portable, not only to VS2003/2005 now, but also to other compilers then.


  • BenK95781

    This behavior is by design. (Any other C++ compiler allowing it is not conforming to the C++ standard.)

    In general, implicit conversions in C++ cannot simultaneously apply the const qualifier and take a reference. I don't know enough about it to give a good justification for it though.

    Brian


  • Possible Bug in Reference to Pointer to Const