wcsstr with a const return type in rc1 header

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/





                                            

Answer this question

wcsstr with a const return type in rc1 header

  • silver23

    Well, yes, okay, but then I don't see anyone taking a strstr on Hello world .  What'd be the point   There is no point.  You use strstr to find a string in a buffer, such as one read from a file, or where ever.  This buffer, obviously, is not const, yet the proto for strstr says the pointer is const.  And like I already said, it's like that so that everyone knows the contents at that ptr are not going to be changed by the routine.  That's it.  The return type has never been const, until rc1.
  • RMD

    I'm looking at page 104, string.h (which I'm including), and it shows no const char*.  By the way, your reference is to an IBM Visual Age PDF manual, a product which is no longer in production, and hasn't been for quite some time. 

    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

    Actually, const doesn't enable compiler optimizations (on the callee side), because at any time, const can be cast away within the routine (the callee).   It is there to facilitate the C++ program design and the compiler front end does type-checking using it, but it isn't a contract to the caller that memory passed to it won't be modified.

    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

    _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

    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

    const means the routine will not change the contents; it allows the compiler some optimization.  It's quite stupid to have string arrays defined as const in practice, unless you want them that way.  The const in the arg list is for the compiler only (and the routine -- that it won't be altering it); it says nothing about the const'ness of the string itself.

    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

    strstr() is also a C++ library routine, and the C++ strstr() is subject to a different standard than C.  Your code would also break if you compiled it under, say, Gnu 4.3.3.  What would you rather have: code based on non-conformant C++ that only compiles under VS 2003, or code that compiles on any platform that obeys the C++ standard





  • Roberto Ki

    Sorry you're having a problem with the const changes we've made in Visual C++ 2005. These changes were made early in the product (and so have been around since at least Beta 1. Here's a summary of what we did, and how you can control it.



    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

    Jumping to the end, what's stupid about it is that I wouldn't need to call wcsstr if it were a const; I'd already know it was in there, and where.  And sure, the reason you const an arg is -dictated by the routine called- . It's there to let everyone know that the routine will not alter the contents of what is at the ptr.  If you don't know this, then it's probably true that few there do, and that's why it's wrong (in the header) and will screw up everybody's source with needless casts.

    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].



  • wcsstr with a const return type in rc1 header