Topic: Default Ctor Question. Where is this in the standard?


Author: petek1976 <pete.karousos@earthlink.net>
Date: Fri, 26 Feb 2010 12:35:03 CST
Raw View
Hi,
Wondering if someone could explain why:

void f()
{
    struct S { // };
    static S s1;
}

Results in an unused varlable warning but

void f()
{
    struct S {//};
    static S s1 = S();
}
does not. What is special about calling the ctor in this way? Where
can I find the rationale for this in the standard? Compiler used is
gcc version 4.3.2 on Redhat Linux.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Pete Becker <pete@versatilecoding.com>
Date: Fri, 26 Feb 2010 17:43:48 CST
Raw View
petek1976 wrote:
> Hi,
> Wondering if someone could explain why:
>
> void f()
> {
>     struct S { // };
>     static S s1;
> }
>
> Results in an unused varlable warning but
>
> void f()
> {
>     struct S {//};
>     static S s1 = S();
> }
> does not. What is special about calling the ctor in this way? Where
> can I find the rationale for this in the standard? Compiler used is
> gcc version 4.3.2 on Redhat Linux.
>

The standard doesn't say anything about warnings. Ask your compiler vendor.

--
    Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
"The Standard C++ Library Extensions: a Tutorial and Reference"
(www.petebecker.com/tr1book)

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Jonathan Mcdougall <jonathanmcdougall@gmail.com>
Date: Fri, 26 Feb 2010 17:45:32 CST
Raw View
> Wondering if someone could explain why:
>
> void f()
> {
>    struct S { // };
>    static S s1;
>
> }
>
> Results in an unused varlable warning but
>
> void f()
> {
>    struct S {//};
>    static S s1 = S();
> }
>
> does not.

Because warnings are specific to implementations. You probably want to
ask in
gcc newsgroups about this. For example, neiher Visual C++ 2008 (with /
W4 /Wall)
nor Comeau online are giving any diagnostic. This is QOI.

> What is special about calling the ctor in this way?

The only difference between these two statements is that the former
uses the
default constructor and the later might use the copy constructor. The
gcc doc
says:

   "-Wunused-variable
         Warn whenever a local variable or non-constant static variable
is unused
         aside from its declaration. [..]"

My guess is that, for the sake of this warning, gcc considers "static
S s1=S();"
to do more than "static S s1;", although both are declarations,
definitions,
construct an object and are single statements. Except for the
(possibly elided)
temporary S(), both are identical.

--
Jonathan Mcdougall

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Fri, 26 Feb 2010 17:43:02 CST
Raw View
On 26 Feb., 19:35, petek1976 <pete.karou...@earthlink.net> wrote:
> Hi,
> Wondering if someone could explain why:
>
> void f()
> {
>     struct S { // };
>     static S s1;
> }
>
> Results in an unused varlable warning but
>
> void f()
> {
>     struct S {//};
>     static S s1 = S();}
>
> does not. What is special about calling the ctor in this way? Where
> can I find the rationale for this in the standard? Compiler used is
> gcc version 4.3.2 on Redhat Linux.

The C++ standard does not answer this question.
A conforming compiler is free to diagnostic any part
of the source code. In fact, the situation is vice versa:
The standard does only specify for some situations
whether compiler *has* to produce a diagnose. The
standard also rules that for some situations no
diagnostic is required.

Your code-snippets doesn't allow to conclude that
it would require a diagnostic, so we are lost here.
Just as an example, Comeau online produces the
following diagnostic for your second example:

"ComeauTest.c", line 4: warning: variable "s1" was declared
but never referenced

   static S s1 = S();
                ^
"

HTH & Greetings from Bremen,

Daniel Kr   gler





--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]