Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is `char* p=0; std::equal(p,p,p)` well-defined according to the C++ standard?

Is the following well-defined according to the C++ standard?

char* p = 0;
std::equal(p, p, p);

The question is really this:

Does the standard require that std::equal(begin1, end1, begin2) is implemented in such a way that if begin1 == end1, then begin1 and begin2 can be any pointer, even one that does not point to a valid memory object?

I assume this is the intention of the standard, but I have not been able to find a statement that makes this clear.

The reason I am concerned about this, is that VisualStudio apparently tries to check the "validity" of begin2 even when begin1 == end1. And that contradicts my understanding of the requirements of the standard.

EDIT: Here is the code from VS 2012 that I believe is in violation of the standard:

template<class _InIt1, class _InIt2> inline
bool equal(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2)
{   // compare [_First1, _Last1) to [First2, ...)
    _DEBUG_RANGE(_First1, _Last1);
    _DEBUG_POINTER(_First2);
    return (_Equal1(_Unchecked(_First1), _Unchecked(_Last1), _First2, _Is_checked(_First2)));
}

template<class _Ty> inline
void _Debug_pointer(const _Ty *_First, _Dbfile_t _File, _Dbline_t _Line)
{   // test iterator for non-singularity, const pointers
    if (_First == 0)
        _DEBUG_ERROR2("invalid null pointer", _File, _Line);
}
like image 477
Kristian Spangsege Avatar asked Oct 01 '13 15:10

Kristian Spangsege


People also ask

What is the difference between char * p and char * p?

char* p and char *p are exactly equivalent. In many ways, you ought to write char *p since, really, p is a pointer to char . But as the years have ticked by, most folk regard char* as the type for p , so char* p is possibly more common.

What is the exact meaning of the following statement int * p char * A?

2 Answers. +3. A pointer to function fp that accepts a pointer to char (open sized string) and returns an integer value.

What does char & P means?

Pointer. In C programming language, *p represents the value stored in a pointer and p represents the address of the value, is referred as a pointer. const char* and char const* says that the pointer can point to a constant char and value of char pointed by this pointer cannot be changed.

What does * p mean in C?

Pointer Airthmetics In C programming language, *p represents the value stored in a pointer.


1 Answers

So we have 25.2.1/1 which says:

Returns: true if for every iterator i in the range [first1,last1) the following corresponding conditions hold: *i == *(first2 + (i - first1)), pred(*i, *(first2 + (i - first1))) != false.

Otherwise, returns false.

In your case there are no iterators in the range [0, 0) so "every" iterator in the range passes the test, but no actual test should be done (since no iterators exist in the range upon which to test).

It looks like a VisualStudio bug to me.

like image 193
Mark B Avatar answered Oct 24 '22 15:10

Mark B