-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
hallo,
I have a odd issue with the current g++ 4.4 release on rawhide.
If I try to pass a string declared as const char * in a c++ programm to the strchr function, I will get an error message which tell me, that a conversation from const char * to char * is not valid.
Because this doesn't happens on older releases of g++, I want to as: Is this a bug or a feature?
Best Regards:
Jochen Schmitt
On Tuesday 10 March 2009 01:26:55 pm Jochen Schmitt wrote:
hallo,
I have a odd issue with the current g++ 4.4 release on rawhide.
If I try to pass a string declared as const char * in a c++ programm to the strchr function, I will get an error message which tell me, that a conversation from const char * to char * is not valid.
Because this doesn't happens on older releases of g++, I want to as: Is this a bug or a feature?
Best Regards:
Jochen Schmitt
My understanding is you can do something like this:
const char *str1, str2; str2 = strchr(str1, 'a');
But str2 must be a const char *.
Maybe I am wrong, I am not very familiar with C++.
Regards,
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Conrad Meyer schrieb:
const char *str1, str2; str2 = strchr(str1, 'a');
But str2 must be a const char *.
Maybe I am wrong, I am not very familiar with C++.
Regards, My understanding is you can do something like this:
I am doing something like this
char *str2; const char *str1;
str2 = strchr(str1, 'a'); // This fails on gcc-4.4
When I write
char *str2; const char *str1; char *temp = const_cast<char *>(str1);
str2 = strchr(temp,'a') // This works
Because this issue occurs only on gcc-4.4, I want to ask: is this a bug or a feature?
Best Regards:
Jochen Schmitt
Jochen Schmitt wrote:
Conrad Meyer schrieb:
const char *str1, str2; str2 = strchr(str1, 'a');
But str2 must be a const char *.
Maybe I am wrong, I am not very familiar with C++.
Regards, My understanding is you can do something like this:
I am doing something like this
char *str2; const char *str1;
str2 = strchr(str1, 'a'); // This fails on gcc-4.4
When I write
char *str2; const char *str1; char *temp = const_cast<char *>(str1);
str2 = strchr(temp,'a') // This works
Because this issue occurs only on gcc-4.4, I want to ask: is this a bug or a feature?
It's a feature.
Andrew.
On Tue, Mar 10, 2009 at 09:42:24PM +0100, Jochen Schmitt wrote:
Because this issue occurs only on gcc-4.4, I want to ask: is this a bug or a feature?
Feature. ISO C++ mandates that std::strchr is overloaded: char *strchr (char *, int); const char *strchr (const char *, int); and similarly for a whole bunch of other {str,mem,wcs}* functions that return the first argument either unmodified or with some offset added to it. While the standard just talks about std namespace and <cstring>/<cwchar> headers, gcc 4.4+ with glibc 2.10+ does that also in <string.h>/<wchar.h> for C++ (not doing it would lead to different behaviour between #include <string.h> #include <cstring> and #include <cstring> #include <string.h> ) and does that also for a bunch of GNU extensions where the overloading makes sense. The overloads make sense, these function no longer cast away constness of pointed types, if you pass a char *, you get char * back, if you pass const char *, you get const char * back.
Jakub
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Jakub Jelinek schrieb:
) and does that also for a bunch of GNU extensions where the overloading makes sense. The overloads make sense, these function no longer cast away constness of pointed types, if you pass a char *, you get char * back, if you pass const char *, you get const char * back.
Thank you for your great explanation. Now I know, that this is a feature and not a bug, so I don't have the need to open a ticket on bugzilla.
Best Regards:
Jochen Schmitt
On Tue, 10 Mar 2009 13:36:07 -0700, Conrad wrote:
My understanding is you can do something like this:
const char *str1, str2; str2 = strchr(str1, 'a');
But str2 must be a const char *.
Maybe I am wrong, I am not very familiar with C++.
In your example, str2 is not a pointer, though. ;)
On Wednesday 11 March 2009 12:17:10 am Michael Schwendt wrote:
On Tue, 10 Mar 2009 13:36:07 -0700, Conrad wrote:
My understanding is you can do something like this:
const char *str1, str2; str2 = strchr(str1, 'a');
But str2 must be a const char *.
Maybe I am wrong, I am not very familiar with C++.
In your example, str2 is not a pointer, though. ;)
Sorry, yes, that should be *str2 in the declaration :). My mistake.
Jochen Schmitt wrote:
I have a odd issue with the current g++ 4.4 release on rawhide.
If I try to pass a string declared as const char * in a c++ programm to the strchr function, I will get an error message which tell me, that a conversation from const char * to char * is not valid.
Because this doesn't happens on older releases of g++, I want to as: Is this a bug or a feature?
In include/c++/4.4.0/cstring you should see:
inline char* strchr(char* __s1, int __n) { return __builtin_strchr(const_cast<const char*>(__s1), __n); }
I wonder if you have not included the correct header.
Andrew.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Andrew Haley schrieb:
Jochen Schmitt wrote:
In include/c++/4.4.0/cstring you should see:
inline char* strchr(char* __s1, int __n) { return __builtin_strchr(const_cast<const char*>(__s1), __n); }
I wonder if you have not included the correct header.
This definition explains my issue, because you can't pass a const char * argument to a char * parameter.
But I'm wondering, why this my issue didn't occured on earlier releases of g++.
Best Regards:
Jochen Schmitt
I have a odd issue with the current g++ 4.4 release on rawhide.
If I try to pass a string declared as const char * in a c++ programm to the strchr function, I will get an error message which tell me, that a conversation from const char * to char * is not valid.
https://www.redhat.com/archives/fedora-devel-list/2009-January/msg02248.html
Cheers, Debarshi