Topic: explicit construction in function return


Author: "Anton & Polina Hristozov" <antonh@earthlink.net>
Date: 2000/02/16
Raw View
Added some changes to your code:

   int main ()
   {
      H h1;  // This line will not compile as the standard says
      H h2(10); // This line will compile
      H h = f (); // This is a bitwise copy (= operator) and it is fine
      return 0;
   }

Hope this helps.

Anton Hristozov


Gerhard Menzl <gerhard.menzl@sea.ericsson.se> wrote in message
news:38A2BF8E.DD96B1BC@sea.ericsson.se...
> The way I interpret the standard, the following code
>
>    struct H
>    {
>       explicit H (int i) : myi (i) {}
>       int myi;
>    };
>
>    H f ()
>    {
>       static int i;
>       return i;
>    }
>
>    int main ()
>    {
>       H h = f ();
>       return 0;
>    }
>
> should be ill-formed because
>
> "An explicit constructor constructs objects just like non-explicit
> constructors, but does so only when the direct initialization syntax
> (8.5) or where casts (5.2.9, 5.4) are explicitly used." (12.3.1/2)
>
> and
>
> "The initialization that occurs in new-expressions (5.3.4), static_cast
> expressions (5.2.9), functional notation type conversions (5.2.3), and
> base and member initializers (12.6.2) is called direct-initialization"
> (8.5)
>
> In the same clause, function return, which is what happens in the code
> above, is listed under copy-initialization. Nevertheless, both compilers
> I am using accept the code. Am I missing something?
>
> Gerhard Menzl
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
>
>


======================================= MODERATOR'S COMMENT:

[ moderator's note: Please do not over-quote. In particular, the
  comp.std.c++ trailer should never be quoted when reply to a posting.
  -sdc ]

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Gerhard Menzl <gerhard.menzl@sea.ericsson.se>
Date: 2000/02/11
Raw View
The way I interpret the standard, the following code

   struct H
   {
      explicit H (int i) : myi (i) {}
      int myi;
   };

   H f ()
   {
      static int i;
      return i;
   }

   int main ()
   {
      H h = f ();
      return 0;
   }

should be ill-formed because

"An explicit constructor constructs objects just like non-explicit
constructors, but does so only when the direct initialization syntax
(8.5) or where casts (5.2.9, 5.4) are explicitly used." (12.3.1/2)

and

"The initialization that occurs in new-expressions (5.3.4), static_cast
expressions (5.2.9), functional notation type conversions (5.2.3), and
base and member initializers (12.6.2) is called direct-initialization"
(8.5)

In the same clause, function return, which is what happens in the code
above, is listed under copy-initialization. Nevertheless, both compilers
I am using accept the code. Am I missing something?

Gerhard Menzl

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Darin Adler <darin@bentspoon.com>
Date: 2000/02/11
Raw View
In article <38A2BF8E.DD96B1BC@sea.ericsson.se>, Gerhard Menzl
<gerhard.menzl@sea.ericsson.se> wrote:

> Nevertheless, both compilers
> I am using accept the code. Am I missing something?

I guess you're missing a compiler that implements this correctly.

The compiler I trie, CodeWarrior 5.3, rejected the return statement with
"Error: function call 'H(int)' does not match 'H::H(int)' 'H::H(const H
&)'". A bad message to be sure, but correct in rejecting the code.

    -- Darin

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Eric Nagler <epn@eric-nagler.com>
Date: 2000/02/12
Raw View
On Fri, 11 Feb 2000 03:27:15 CST, Gerhard Menzl
<gerhard.menzl@sea.ericsson.se> wrote:

>The way I interpret the standard, the following code
>
>   struct H
>   {
>      explicit H (int i) : myi (i) {}
>      int myi;
>   };
>
>   H f ()
>   {
>      static int i;
>      return i;
>   }
>
>   int main ()
>   {
>      H h = f ();
>      return 0;
>   }
>
>should be ill-formed because
>
>"An explicit constructor constructs objects just like non-explicit
>constructors, but does so only when the direct initialization syntax
>(8.5) or where casts (5.2.9, 5.4) are explicitly used." (12.3.1/2)
>
>and
>
>"The initialization that occurs in new-expressions (5.3.4), static_cast
>expressions (5.2.9), functional notation type conversions (5.2.3), and
>base and member initializers (12.6.2) is called direct-initialization"
>(8.5)
>
>In the same clause, function return, which is what happens in the code
>above, is listed under copy-initialization. Nevertheless, both compilers
>I am using accept the code. Am I missing something?
>
>Gerhard Menzl

FWIW, BCB4 rejects the code above. VC++ 6.0 accepts it.

EriC++

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/02/12
Raw View
In article <38A2BF8E.DD96B1BC@sea.ericsson.se>, Gerhard Menzl <gerhard.m
enzl@sea.ericsson.se> writes
>In the same clause, function return, which is what happens in the code
>above, is listed under copy-initialization. Nevertheless, both compilers
>I am using accept the code. Am I missing something?

I think (actually sure) you are right and the compilers should be
issuing a diagnostic (even if they continue to generate executable code)



Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]