Board index » cppbuilder » Why W8105 warning on const in typedef struct fields?

Why W8105 warning on const in typedef struct fields?


2008-03-20 04:29:33 AM
cppbuilder93
I'm migrating code from MS VS 2003 and gcc and looking at all the warnings and errors that the latest Borland compiler version (v5.9.2 with BCB 2007 Dec 2007 service pak).
I see this warning below. Then I also show how we use it. We declare an array of initialized members. The idea is that they are const since once declared initialized they never get changed. Why should this be a warning? This was a perfectly natural pre-C++ thing to do in C. It still seems a natural use of arrays of structs.
Is there another way to accomplish this objective and still enforce const once the array is initialized?
[BCC32 Warning] ServerSocket.h(52): W8105 Constant member ' ::uiErrorCode' in class without constructors
typedef struct {
const unsigned int uiErrorCode;
const char* pErrorDesc;
}WinsockError_t;
WinsockError_t WinsockErrors [] = {
{WSAEINTR, "Interrupted function call"},
{WSAEACCES, "Permission denied"},
{WSAEFAULT, "Bad address"},
etc.....
 
 

Re:Why W8105 warning on const in typedef struct fields?

"Randall Parker" < XXXX@XXXXX.COM >writes:
Quote
I'm migrating code from MS VS 2003 and gcc and looking at all the
warnings and errors that the latest Borland compiler version (v5.9.2
with BCB 2007 Dec 2007 service pak).

I see this warning below. Then I also show how we use it. We declare
an array of initialized members. The idea is that they are const
since once declared initialized they never get changed. Why should
this be a warning? This was a perfectly natural pre-C++ thing to do
in C. It still seems a natural use of arrays of structs.

Is there another way to accomplish this objective and still enforce
const once the array is initialized?

[BCC32 Warning] ServerSocket.h(52): W8105 Constant member '
::uiErrorCode' in class without constructors

typedef struct {
const unsigned int uiErrorCode;
const char* pErrorDesc;
}WinsockError_t;

WinsockError_t WinsockErrors [] = {
{WSAEINTR, "Interrupted function call"},
{WSAEACCES, "Permission denied"},
{WSAEFAULT, "Bad address"},
etc.....
For types that are eligable for aggregate initialization, this warning
clearly makes no sense. I think this is a case of the compiler trying
to be helpful but not checking enough of the situation before
complaining.
Is disabling the warning in that situation a viable workaround?
--
Chris (TeamB);
 

Re:Why W8105 warning on const in typedef struct fields?

"Chris Uzdavinis (TeamB)" < XXXX@XXXXX.COM >wrote:
Quote
"Randall Parker" < XXXX@XXXXX.COM >writes:
>[BCC32 Warning] ServerSocket.h(52): W8105 Constant member '
>::uiErrorCode' in class without constructors
>
>typedef struct {
>const unsigned int uiErrorCode;
>const char* pErrorDesc;
>}WinsockError_t;
>
>WinsockError_t WinsockErrors [] = {
>{WSAEINTR, "Interrupted function call"},
>{WSAEACCES, "Permission denied"},
>{WSAEFAULT, "Bad address"},
>etc.....


For types that are eligable for aggregate initialization, this warning
clearly makes no sense. I think this is a case of the compiler trying
to be helpful but not checking enough of the situation before
complaining.

Is disabling the warning in that situation a viable workaround?
Chris,
Yes, I can turn off the warning. Though I naturally wish to avoid doing that in case the warning might catch another problem some day.
Are warnings deactivations stored in a project, in a project group, or globally?
Another idea: I could declare the array variable as const and remove the const declares from each of the fields in the struct. In this case it is really when I go do declare the array that I know it is full of constants.
Quote

--
Chris (TeamB);
 

{smallsort}

Re:Why W8105 warning on const in typedef struct fields?

Randall Parker wrote:
[snip]
Quote
Are warnings deactivations stored in a project, in a project group, or globally?

Per-project.
 

Re:Why W8105 warning on const in typedef struct fields?

"Randall Parker" < XXXX@XXXXX.COM >wrote in message
Quote

"Chris Uzdavinis (TeamB)" < XXXX@XXXXX.COM >wrote:
>"Randall Parker" < XXXX@XXXXX.COM >writes:

>>[BCC32 Warning] ServerSocket.h(52): W8105 Constant member '
>>::uiErrorCode' in class without constructors
>>
>>typedef struct {
>>const unsigned int uiErrorCode;
>>const char* pErrorDesc;
>>}WinsockError_t;
>>
>>WinsockError_t WinsockErrors [] = {
>>{WSAEINTR, "Interrupted function call"},
>>{WSAEACCES, "Permission denied"},
>>{WSAEFAULT, "Bad address"},
>>etc.....
>
>
Yes, I can turn off the warning. Though I naturally wish to avoid doing that
in case the warning might catch another problem some day.

Are warnings deactivations stored in a project, in a project group, or
globally?

You could add this to the file in question:
#pragma warn -8105
--
Bruce
 

Re:Why W8105 warning on const in typedef struct fields?

Bruce Salzman wrote:
Quote
You could add this to the file in question:

#pragma warn -8105
And then turn it back on after the offending code.
However, I would prefer to make the variable const
instead of the typedef.
 

Re:Why W8105 warning on const in typedef struct fields?

"Randall Parker" < XXXX@XXXXX.COM >writes:
Quote
Chris,

Yes, I can turn off the warning. Though I naturally wish to avoid
doing that in case the warning might catch another problem some day.
Those kind of options are stored on an "option stack". That is, you
can push a pragma onto the stack to have effect over a scope, and when
you no longer want that option, you can pop it back off and the
options revert to what it was.
That's why you see something like #pragma option push .... and
#pragma option pop in code. The thing being pushed only affects the
code in between those push/pop pairs.
That way the warning is disabled ONLY for the declaration of that
particular data structure.
Quote
Are warnings deactivations stored in a project, in a project group,
or globally?
Oh, I wasn't suggesting to disable the warning from the IDE, or it is
globally disabled, which as you suggested, is probably too strong of a
request. (But I don't know how the IDE stores that setting. I would
guess it's part of the project, but beyond that, I haven't used the
IDE in several years.)
Quote
Another idea: I could declare the array variable as const and remove
the const declares from each of the fields in the struct. In this
case it is really when I go do declare the array that I know it is
full of constants.
You mean something like this:
typedef struct {
unsigned int uiErrorCode;
char * pErrorDesc;
}WinsockError_t;
WinsockError_t const WinsockErrors [] = {
{WSAEINTR, "Interrupted function call"},
{WSAEACCES, "Permission denied"},
{WSAEFAULT, "Bad address"},
... };
?
That will compile but not be entirely helpful. "Const" is a shallow
qualifier. That is if WinsockErrors is an array of const objects,
then the elements are const, meaning their members are const. But
that's as far as it goes.
You wouldn't be able to change WinsockErrors.uiErrorCode or
WinsockErrors.pErrorDesc, but you COULD change member that
WinsockErrors.pErrorDesc points to.
That is, the pointer pErrorDesc is const, but the data to which it
points is NOT const.
In any case, whether you leave the const qualifier on the elements of
WinsockError_t, you probably want to make WinsockErrors instance be
const in any case. THis will prevent someone from being able to
replace your description pointers to point to different description
strings.
--
Chris (TeamB);
 

Re:Why W8105 warning on const in typedef struct fields?

Bob Gonder < XXXX@XXXXX.COM >writes:
Quote
Bruce Salzman wrote:

>You could add this to the file in question:
>
>#pragma warn -8105

And then turn it back on after the offending code.

However, I would prefer to make the variable const
instead of the typedef.
See my other reply. That would make the pointer const, but not the
data to which it points. I think really in this kind of scenario,
both const pointer and const data is desired.
--
Chris (TeamB);
 

Re:Why W8105 warning on const in typedef struct fields?

Chris Uzdavinis (TeamB) wrote:
Quote
"Randall Parker" < XXXX@XXXXX.COM >writes:

>Chris,
>
>Yes, I can turn off the warning. Though I naturally wish to avoid
>doing that in case the warning might catch another problem some day.

Those kind of options are stored on an "option stack". That is, you
can push a pragma onto the stack to have effect over a scope, and when
you no longer want that option, you can pop it back off and the
options revert to what it was.
Is this what you are suggesting? In other words, is this the correct way to do it?
#pragma option push warn -8105
typedef struct {
const unsigned int uiErrorCode;
const char* pErrorDesc;
}WinsockError_t;
#pragma option pop warn -8105
Quote

That's why you see something like #pragma option push .... and
#pragma option pop in code. The thing being pushed only affects the
code in between those push/pop pairs.

That way the warning is disabled ONLY for the declaration of that
particular data structure.
 

Re:Why W8105 warning on const in typedef struct fields?

Randall Parker wrote:
Quote
Is this what you are suggesting? In other words, is this the correct way to do it?

#pragma option push warn -8105
typedef struct {
const unsigned int uiErrorCode;
const char* pErrorDesc;
}WinsockError_t;
#pragma option pop warn -8105
Close...
#pragma option push -w-8105
typedef struct{....}WinsockError_t;
#pragma option pop