Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minor (unimportant) defect in the standard?

This question has no practical issues associated with it, it is more a matter of curiosity and wanting to know if I am taking things too literally ;).

So I have been trying to work towards understanding as much of the c++ standard as possible. Today in my delving into the standard I noticed this (ISO/IEC 14882:2003 21.3.4):

const_reference operator[](size_type pos) const;
reference operator[](size_type pos);
Returns: If pos < size(), returns data()[pos].
         Otherwise, if pos == size(), the const version returns charT().
         Otherwise, the behavior is undefined.

Seems pretty sane to me. But then I thought to myself, wait a sec what's the definition of data()?.

const charT* data() const;

yup, it returns a const charT*.

Clearly the non-const version of operator[] cannot be implemented as a simple return data()[pos] then since that would be initializing a reference of type char& from an expression of type const char.

I think that it is obvious that the intent is that data() be implemented something like return data_; and operator[] be implemented as return data_[pos]; or something functionally similar, but that's not what the standard says :-P.

If I recall correctly, implementors have some leeway in that they can implement things how they please as long as it meets the basic requirements given and has the same net effect.

So the question is, am I being way too literal, or is this the type of thing that would be considered a defect.

EDIT: It is worth noting that the c++0x draft has changed the wording to:

Returns: If pos < size(), returns *(begin() + pos).
         Otherwise, if pos == size(), the const version returns charT().
         Otherwise, the behavior is undefined.

So perhaps I have just stumbled onto something that has already been discussed.

like image 446
Evan Teran Avatar asked Jul 06 '10 19:07

Evan Teran


People also ask

What is a minor defect mean?

A Minor defect is a discrepancy from the standards, but one that is not likely to affect the usability of an object. A Major defect is one that is likely to create failure of the unit for its intended purpose. A Critical defect is one that is deemed to be hazardous or unsafe.

Which kind of defect can be called as minor defect?

Minor Defect – A defect that does not reduce the usability of the product but is nevertheless a workmanship defect beyond the defined specifications or construction requirements. Tolerance limits for defect (size, repeatability, quality, significance) will be defined later. 4 Minor defects -> 1 Major Defect.

What is a minor defect example?

An example of a minor defect might be a small (up to 1.5″) scratch on the bottom of the product. Pro tip: The more specific the product criteria is, the less subjective the nature of the defects. Providing photos as examples is particularly useful.


2 Answers

Yes, it was a defect and yes, this was the fix.

http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-defects.html#259

like image 178
CB Bailey Avatar answered Oct 07 '22 22:10

CB Bailey


I assume that they used data() in the definition instead of data_ becuase they wanted to define in strictly in terms of the public interface.

like image 29
James Curran Avatar answered Oct 07 '22 20:10

James Curran