Hi 40th Floor! > _CRTIMP __checkReturn _CONST_RETURN wchar_t * __cdecl wcsstr(__in_z > const wchar_t * _Str, __in_z const wchar_t * _SubStr); > > What's the deal with the "const" on the return in > > ...\Microsoft Visual Studio 8\VC\include\string.h > > when the docs I see show > > *wchar_t *wcsstr( const wchar_t* */_string_/, *const* *wchar_t* > */_strCharSet_/ *);* It seems that the docs (or the standard) are wrong :-) If the parameter is const then the return value must also be const. If the return valus would not be const, this could lead to GPFs... -- Greetings Jochen My blog about Win32 and .NET http://blog.kalmbachnet.de/

wcsstr with a const return type in rc1 header
silver23
RMD
Anyway, strstr() is a c library routine. Always has been. const char * is used in the prototype for the reason I stated, which doesn't seem to be getting across, as obvious as it is. (Why do you think the string routines have returned non-const these past decades , and also why const char* is used for the first arg )
No, I didn't bring up literals. That's not the point, not even close. The point is the header is messing up a lot of code. Many string routines are screwed up now, not just strstr/wcststr.. Can you imagine how many source files are now broken, because of this
Glamdring
Note that Whole Program Optimization, aka /LTCG, does do cross-procedural optimizations that make use of what memory gets read and written to, but this information is independent of the use of const.
There are a set of functions in the VS headers, like strstr, that are prototyped two ways under C++. One that takes a non-const pointer and returns a non-const pointer, and one that takes a const pointer and returns a const pointer. This makes sense since strstr is returning a pointer into the string given to it. The dual prototype *has* to be there in order to maintain the constness of the string array going into the function and out of it.
I'm not sure what's "stupid" about defining string arrays as const. Both const and unconst arrays have their uses.
neris
The best citation you're after is not from MSFT, but from the ISO C++ Standard which was found carved into a stone tablet on Mount Sinai. On page 108 of this particular copy of the standard
http://www-1.ibm.com/support/docview.wss uid=swg27002104&aid=1 (my first google hit) you will see an overload of strstr() that returns const char*.
Back to the middle, I think you might be confusing literal strings with const strings (synonymous to const char arrays). They are different. When I write code, I want to give certain functions read-only access to strings, and certain functions write-access. Make note that not all const strings are literal strings. e.g. I can use rand() to generate my characters, pass it around as a const char*, and use strstr() to search it.
With the existence of both kinds of functions (those taking non-const strings and those taking const strings), I need both variations of strstr() in order to use that function in both contexts.
liranbh
What's the deal with the "const" on the return in
...\Microsoft Visual Studio 8\VC\include\string.h
05 08 30 20:58 26,028 string.h
of RC1,
when the docs I see show
wchar_t *wcsstr( const wchar_t *string, const wchar_t *strCharSet );
DriverDream
Can you cite an authority/reference where the return type is const (w)char* for strstr/wcsstr I have never seen one. I can't find one. Well, except in the header of rc1, but that's not an authority, and frankly, not a clear reference at all.
Fabrikx74
Roberto Ki
We made the declarations of our string functions conformant to the C++ standard [21.4] rather than the C standard [7.21.5.7]. Rather than have a difference between C and C++, we choose to make the const return type present for both C and C++. When this kind of const return is present we define _CRT_CONST_CORRECT_OVERLOADS.
When we add the new const-correct versions, we also add non-const overloads, meaning that both of these will work:
char *p, *q, *r;
const char *cp, *cq, *cr;
p=strstr(q,r);
cp=strstr(cq,cr);
If you don't like this behaviour, you can add /D_CONST_RETURN to your command line, and we won't return const types, just as we did in earlier versions.
Martyn Lovell
Development Lead
Visual C++ Libraries
HemantKumar
Back to the start, can you cite a reference where anyone else uses this const return type for strstr/wcsstr Outside of MSFT that is.
bobhen
Note: in C++ the type of "Hello World" is const char[12].