Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to "fake" a member initialization?

44 views
Skip to first unread message

Paulo da Silva

unread,
Jan 13, 2020, 1:53:28 PM1/13/20
to
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?

Thanks

Öö Tiib

unread,
Jan 13, 2020, 2:08:19 PM1/13/20
to
Your description is confusing. You do not know structure of a
member but compiler apparently knows, otherwise it is ill formed
program that should give somewhat different diagnostic. How does
compiler know something that you do not know? So better post actual,
compiling example of what you mean with your question.


Bonita Montero

unread,
Jan 13, 2020, 2:12:54 PM1/13/20
to
Iy your member is templated use
type member {};
in the class-definition.

Jorgen Grahn

unread,
Jan 13, 2020, 3:15:33 PM1/13/20
to
Yes, by not initializing that member, just like you're already doing.

Does the compiler also complain about this?

struct Foo {
int i,j;
Foo() : i(0) {}
};

Then I think the problem is you have enabled too many warning options
of the kind "maybe the author meant something else; better warn".

What's the compiler and what options do you enable? I have seen that
warning somewhere, but the options I normally use (g++ -Wall -Wextra
-pedantic) doesn't enable it.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Paulo da Silva

unread,
Jan 13, 2020, 3:32:15 PM1/13/20
to
Às 18:53 de 13/01/20, Paulo da Silva escreveu:
From the answers so far, may be I didn't explain my problem very well :(

The main point is that I have a member (a var) which I don't want to
initialize in the constructor. So, the compiler warns me of that.
What is the best practice to "clean" that warning message for that
particular member?

Not important, btw, the reasons I don't want to initialize it are:
1. It is not needed;
2. That member is an external structure that depends on the OS.

I could do for example:
memset((void *)&foo,0,sizeof(foo));
or, better
memset((void *)&foo,0,0);
1. How do I do this, in a more c++ way?
2. Is there a better way to fool the compiler without doing anything?

Thanks.

Jorgen Grahn

unread,
Jan 13, 2020, 3:36:12 PM1/13/20
to
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?

Paulo da Silva

unread,
Jan 13, 2020, 3:59:53 PM1/13/20
to
À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.

Now that I have read it ...

About the compiler: It's g++ and all options are appropriate for the
rest of the project.

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.
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++?

Melzzzzz

unread,
Jan 13, 2020, 4:28:20 PM1/13/20
to
Live warning for unaware before making more serious mistake....


--
press any key to continue or any other to quit...
U ničemu ja ne uživam kao u svom statusu INVALIDA -- Zli Zec
Svi smo svedoci - oko 3 godine intenzivne propagande je dovoljno da jedan narod poludi -- Zli Zec
Na divljem zapadu i nije bilo tako puno nasilja, upravo zato jer su svi
bili naoruzani. -- Mladen Gogala

Melzzzzz

unread,
Jan 13, 2020, 4:30:05 PM1/13/20
to
On 2020-01-13, Paulo da Silva <p_s_d_a_s_...@netcabo.pt> wrote:
I guess so...

> Is it the best way in c++?





Jorgen Grahn

unread,
Jan 13, 2020, 4:45:31 PM1/13/20
to
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.

Alf P. Steinbach

unread,
Jan 13, 2020, 5:24:01 PM1/13/20
to
Post code that reproduces the problem.

Preferably a minimal example that you make and test.

Quote the exact warning (it looks like you've tried to do that but made
some generalization, don't, quote the exact text) and which dang fool
tool issued it. Apparently it's an Eclipse thing, not a compiler thing.
Another possible non-compiler fool tool source is the Visual Studio
Intellisense engine.

If it is Eclipse then consider using a different editor/IDE, or
upgrading to a newer version.

At a guess that would solve the problem, but it would be nice if you
could report back here if it did.


- Alf


Paulo da Silva

unread,
Jan 13, 2020, 8:04:59 PM1/13/20
to
Às 21:45 de 13/01/20, Jorgen Grahn escreveu:
> On Mon, 2020-01-13, Paulo da Silva wrote:
...

>>>>
>>>> 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.
>
Yes, I needed to leave the computer for a while when responding.

>> 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".
No, but this is a moderate sized project, not new, and I want to keep
those warnings, if there are members uninitialized, except for special
cases like this.

> ...

>> 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.
So do I. That's the main reason why I asked for suggestions.

>
>> 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"?
Of course I could just ignore the warning, but I became curious on how
to "clean" just this one. I also like clean compilations.

Thanks.

Frederick Gotham

unread,
Jan 14, 2020, 3:34:20 AM1/14/20
to
You need to turn the warning off just for that one line.

You "push" the current compiler warning settings, you disable that one warning, you do your line of code, and then you "pop" all the warnings back the way they were.

Like this:

int main(void)
{
int i;

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuninitialized"
int j = i + 5;
#pragma GCC diagnostic pop

return 0;
}

If you want a portable way of doing this, look for "Hedley":

https://nemequ.github.io/hedley/user-guide.html

Paulo da Silva

unread,
Jan 14, 2020, 10:33:21 AM1/14/20
to
Às 08:34 de 14/01/20, Frederick Gotham escreveu:
This is worth seeing.

Thank you.
0 new messages