[Piglit] [PATCH 3/3] util: Fix `cannot convert from 'const char *' to 'char *'` MSVC error.

Jose Fonseca jfonseca at vmware.com
Wed Dec 3 14:34:40 PST 2014


On 03/12/14 22:26, Jan Vesely wrote:
> On Wed, 2014-12-03 at 15:37 +0000, Jose Fonseca wrote:
>> From: José Fonseca <jfonseca at vmware.com>
>>
>> When in C++ mode, MSVC defines overloaded versions of standard names
>> that preserve constness. :-/
>>
>> https://urldefense.proofpoint.com/v2/url?u=http-3A__msdn.microsoft.com_en-2Dus_library_b34ccac3.aspx&d=AAIFaQ&c=Sqcl0Ez6M0X8aeM67LKIiDJAXVeAw-YihVMNtXt-uEs&r=zfmBZnnVGHeYde45pMKNnVyzeaZbdIqVLprmZCM2zzE&m=kuuSkc8jlnYV0Y19w3u4anqzpUxOnVf0Tq2Mncnvhlo&s=VOrlzvSdpo34lv3NhFdrMOEs325YxKvSuEHTzZO_9Sw&e=
>> ---
>>   tests/util/piglit-util.h | 4 ++++
>>   1 file changed, 4 insertions(+)
>>
>> diff --git a/tests/util/piglit-util.h b/tests/util/piglit-util.h
>> index 099075a..af9fb97 100755
>> --- a/tests/util/piglit-util.h
>> +++ b/tests/util/piglit-util.h
>> @@ -211,7 +211,11 @@ strtod_inf(const char *nptr, char **endptr)
>>   static inline char *
>>   strchrnul(const char *s, int c)
>>   {
>> +#if defined(_MSC_VER) && defined(__cplusplus)
>> +	char *t = strchr((char *)s, c);
>
> shouldn't the above version work in all cases? Implicit conversion to
> const should be ok with other compilers, or am I missing something?

Yes,  just `char *t = strchr((char *)s, c);` should work everywhere.

But _something_ more was needed to ensure that a few months later 
somebody looking at the code wouldn't just yank the '(char *)' case out.

I could have added a comment, but it seemed to me that the `#if 
defined(_MSC_VER) && defined(__cplusplus)` line described much more 
succinctly and accurately why that cast was necessary than I could do in 
English.

Jose




More information about the Piglit mailing list