Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #71310 > unrolled thread

How to "fake" a member initialization?

Started byPaulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt>
First post2020-01-13 18:53 +0000
Last post2020-01-14 15:33 +0000
Articles 14 — 7 participants

Back to article view | Back to comp.lang.c++


Contents

  How to "fake" a member initialization? Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt> - 2020-01-13 18:53 +0000
    Re: How to "fake" a member initialization? Öö Tiib <ootiib@hot.ee> - 2020-01-13 11:08 -0800
    Re: How to "fake" a member initialization? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-13 20:12 +0100
    Re: How to "fake" a member initialization? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-13 20:15 +0000
    Re: How to "fake" a member initialization? Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt> - 2020-01-13 20:32 +0000
      Re: How to "fake" a member initialization? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-13 20:36 +0000
        Re: How to "fake" a member initialization? Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt> - 2020-01-13 20:59 +0000
          Re: How to "fake" a member initialization? Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-13 21:29 +0000
          Re: How to "fake" a member initialization? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-13 21:45 +0000
            Re: How to "fake" a member initialization? Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt> - 2020-01-14 01:04 +0000
      Re: How to "fake" a member initialization? Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-13 21:28 +0000
    Re: How to "fake" a member initialization? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2020-01-13 23:23 +0100
    Re: How to "fake" a member initialization? Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-14 00:34 -0800
      Re: How to "fake" a member initialization? Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt> - 2020-01-14 15:33 +0000

#71310 — How to "fake" a member initialization?

FromPaulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt>
Date2020-01-13 18:53 +0000
SubjectHow to "fake" a member initialization?
Message-ID<qvieas$1dah$1@gioia.aioe.org>
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

[toc] | [next] | [standalone]


#71312

FromÖö Tiib <ootiib@hot.ee>
Date2020-01-13 11:08 -0800
Message-ID<d8200bb3-81ec-4ad4-a10c-608d4417112a@googlegroups.com>
In reply to#71310
On Monday, 13 January 2020 20:53:28 UTC+2, Paulo da Silva  wrote:
> 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?

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.

[toc] | [prev] | [next] | [standalone]


#71313

FromBonita Montero <Bonita.Montero@gmail.com>
Date2020-01-13 20:12 +0100
Message-ID<qviffc$4gk$1@news.albasani.net>
In reply to#71310
Am 13.01.2020 um 19:53 schrieb Paulo da Silva:
> 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?

Iy your member is templated use
    type member {};
in the class-definition.

[toc] | [prev] | [next] | [standalone]


#71314

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2020-01-13 20:15 +0000
Message-ID<slrnr1pjuq.km3.grahn+nntp@frailea.sa.invalid>
In reply to#71310
On Mon, 2020-01-13, Paulo da Silva wrote:
> 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?

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   .

[toc] | [prev] | [next] | [standalone]


#71315

FromPaulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt>
Date2020-01-13 20:32 +0000
Message-ID<qvik43$8ki$1@gioia.aioe.org>
In reply to#71310
À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 :(

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.

[toc] | [prev] | [next] | [standalone]


#71316

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2020-01-13 20:36 +0000
Message-ID<slrnr1pl5h.km3.grahn+nntp@frailea.sa.invalid>
In reply to#71315
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?

/Jorgen

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

[toc] | [prev] | [next] | [standalone]


#71318

FromPaulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt>
Date2020-01-13 20:59 +0000
Message-ID<qvilns$fml$1@gioia.aioe.org>
In reply to#71316
À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++?

[toc] | [prev] | [next] | [standalone]


#71320

FromMelzzzzz <Melzzzzz@zzzzz.com>
Date2020-01-13 21:29 +0000
Message-ID<pj5TF.39955$%G5.5192@fx03.am4>
In reply to#71318
On 2020-01-13, Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt> 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.
>
> 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?

I guess so...

> Is it the best way in c++?





-- 
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

[toc] | [prev] | [next] | [standalone]


#71321

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2020-01-13 21:45 +0000
Message-ID<slrnr1pp7i.km3.grahn+nntp@frailea.sa.invalid>
In reply to#71318
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.

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

[toc] | [prev] | [next] | [standalone]


#71327

FromPaulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt>
Date2020-01-14 01:04 +0000
Message-ID<qvj43e$dia$1@gioia.aioe.org>
In reply to#71321
À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.

[toc] | [prev] | [next] | [standalone]


#71319

FromMelzzzzz <Melzzzzz@zzzzz.com>
Date2020-01-13 21:28 +0000
Message-ID<Lh5TF.39954$%G5.36348@fx03.am4>
In reply to#71315
On 2020-01-13, Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt> 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 :(
>
> 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.

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

[toc] | [prev] | [next] | [standalone]


#71323

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2020-01-13 23:23 +0100
Message-ID<qviqlo$j5q$1@dont-email.me>
In reply to#71310
On 13.01.2020 19:53, Paulo da Silva wrote:
> 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?

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

[toc] | [prev] | [next] | [standalone]


#71332

FromFrederick Gotham <cauldwell.thomas@gmail.com>
Date2020-01-14 00:34 -0800
Message-ID<ff24deb2-8021-4afd-9d5d-57b6021fd423@googlegroups.com>
In reply to#71310
On Monday, January 13, 2020 at 6:53:28 PM UTC, Paulo da Silva wrote:
> 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


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

[toc] | [prev] | [next] | [standalone]


#71342

FromPaulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt>
Date2020-01-14 15:33 +0000
Message-ID<qvkmvm$1g0c$1@gioia.aioe.org>
In reply to#71332
Às 08:34 de 14/01/20, Frederick Gotham escreveu:
> On Monday, January 13, 2020 at 6:53:28 PM UTC, Paulo da Silva wrote:
>> 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
> 
> 
> 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
> 
This is worth seeing.

Thank you.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web