Search

std::find specialization by PowerGamer1

Closed
as Fixed Help for as Fixed

1
1
Sign in
to vote
Type: Bug
ID: 757385
Opened: 8/7/2012 12:44:19 PM
Access Restriction: Public
0
Workaround(s)
0
User(s) can reproduce this bug
When using std::find with "char" data type, in many cases for-based implementation of std::find is invoked instead of memchr one. To force compiler to use memchr-based implementation of std::find in such a case (see example program) one must use a total of THREE static_casts! This is completely counter-intuitive and absurd.

In all situations when using "char" data type std::find should use memchr.
Details (expand)

Visual Studio/Team Foundation Server/.NET Framework Tooling Version

Visual Studio 2012 RC

Steps to reproduce

#include <algorithm>
int main()
{
char str[] = "test";
std::find(str, str+sizeof(str), 's'); // for-based std::find instead of memchr-based
std::find(static_cast<const char*>(str), static_cast<const char*>(str)+sizeof(str), static_cast<int>('s')); // forcing to use memchr-based implementation
}

Product Language

English

Operating System

Windows 7

Operating System Language

English

Actual results

Single step program in debug to see that first call of std::find uses generic for-based implementation of std::find.

Expected results

Not only the second but first call to std::find should use memchr specialization of std::find.
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 10/9/2012 at 4:58 PM
Hi,

Thanks for reporting this bug. We've fixed it, and the fix will be available in the next update to our C++ Standard Library implementation.

I rewrote find() so it's maximally aggressive about activating the memchr() optimization. Specifically, it's now activated when all of these stars align:

* The given iterators are pointers, or things that the STL knows how to unwrap to pointers (this applies to std::vector, std::string, and std::array iterators).

* The elements are char, signed char, or unsigned char. They can be either modifiable or const.

* The given value is integral (of any signedness and any width).

I also fixed a surprising correctness bug in the old implementation. Given a signed char range containing -1, find() should report that 255 is not found within the range, because static_cast<signed char>(-1) != 255. memchr() happily considers them to be equal. We now perform range checks to adapt memchr()'s behavior to find()'s. This includes the special case where, for example, 0xFFFFFFFFUL is outside the [-128, 127] range of signed char, but is equal to the signed char -1 anyways (thanks to the usual arithmetic conversions).

Note: Connect doesn't notify me about comments. If you have any further questions, please E-mail me.

Stephan T. Lavavej
Senior Developer - Visual C++ Libraries
stl@microsoft.com
Posted by Microsoft on 8/7/2012 at 7:04 PM
Thank you for submitting feedback on Visual Studio 11 and .NET Framework. Your issue has been routed to the appropriate VS development team for review. We will contact you if we require any additional information.
Posted by Microsoft on 8/7/2012 at 12:51 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.