On Mon, 2020-01-13, Paulo da Silva wrote:
> Às 20:36 de 13/01/20, Jorgen Grahn escreveu:
>> On Mon, 2020-01-13, Paulo da Silva wrote:
>>> Às 18:53 de 13/01/20, Paulo da Silva escreveu:
>>>> Hi all!
>>>>
>>>> I have a member whose structure I don't know.
>>>> I don't need it to be initialized but the compiler warns me of that.
>>>> "Member 'foo' was not initialized in this constructor".
>>>>
>>>> Is there a way to tell the compiler that I don't want that member to be
>>>> initialized?
>>>>
>>>
>>> From the answers so far, may be I didn't explain my problem very well :(
>>
>> What was wrong with my answer?
>>
> Sorry, I hadn't seen your answer when I posted.
Come to think of it, I posted it only 20--30 minutes before you posted
so I shouldn't have assumed you had.
> Now that I have read it ...
>
> About the compiler: It's g++ and all options are appropriate for the
> rest of the project.
I must assume that means "it's at work, so I cannot disclose that
information".
I'll also assume my example with a simple uninitialized int member
would have triggered the warning.
There's a g++ -Weffc++ option which would have triggered that warning,
but with different wording. I tried that one many years ago, but
rejected it as too restrictive.
> I have this code:
> Dir::Dir(): cur_entryp(0),vec(0)
> {
> // memset((void *)&cur_dev,0,sizeof(cur_dev)); // OK
> // memset((void *)&cur_dev,0,0); // OK and do nothing.
I strongly dislike the first two -- they bring in C style thinking,
and would happily and silently trash cur_dev if it ever got a real
constructor.
> cur_dev={}; // OK
>
> ... other init stuff ...
>
> }
>
> The member I am talking about is cur_dev.
> cur_dev={} also works (no warning)!
> Does this cause any code to be executed?
> Is it the best way in c++?
Personally, I think it's fine to have uninitialized members[0], and
the only good solution I can see is to change the compiler options.
But if you cannot make that decision, I think you should go to whoever
makes those decisions and ask "well, how do you want me to handle this
situation, then"?
For certain warnings there are good and well-known ways to shut them
up without changing the logic, e.g.
- unused parameters (don't name them)
- falling through to the next case in a switch (/* FALLTHROUGH */)
but I don't think there is one for this case. (Well, I can't be sure
since I don't even know which option enables the warning[1].)
/Jorgen
[0] Although I prefer to have members with real constructors, and even
avoid raw int and bool members in larger classes.