the ptrdiff - the ptrdiff_t kludge for operator Jonathan...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
the ptrdiff_t kludge for operator[] Jonathan Schilling, jls@sco.com Consider the following innocent-looking fragment of a C++ string class: class String { public: String(); char& operator[](unsigned int); operator const char*(); }; The class has an indexing operator to get at characters of the String, and there's also a conversion operator to convert the String into a C-level const char* string. A typical usage might be: String s; char c = s[0]; But there is a potential ambiguity here. Is the expression s[0] equivalent to: s.operator[](0) or: ((const char*)s)[0] The second alternative is more strictly: (s.operator const char*())[0] The intuitive answer is the first alternative, and for a long time compilers interpreted it as such. This was relying on a nonstandard weighting of operator[] in overload resolution, however, and beginning two or three years ago compilers began implementing this according to the draft ANSI/ISO standard, in which it was ambiguous and an error. It was ambiguous because the first interpretation involves an exact match (String s,
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 2

the ptrdiff - the ptrdiff_t kludge for operator Jonathan...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online