Topic: is this a correct C++ declaration ?


Author: Bill Seymour <wseymour@email.usps.gov>
Date: 1996/05/06
Raw View
marek@iiasa.ac.at (Marek  MAKOWSKI) wrote:
>I would appreciate your comment on the correctness of the
>following declaration:
>
>template <class Left, class Right> class mPair {
>public:
>   mPair(const Left& left_v = (Left) 0, const Right& right_v = (Right) 0)
>  : left_(left_v), right_(right_v) {}
>  // details removed
>private:
>   Left left_;
>   Right right_;
>};
>

You've declared the constructor arguments as references,
not as pointers; and there's no such thing as a null
reference.  If Left and Right have one-argument constructors
that take ints, there's no syntax error.

--Bill
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Marco_Dalla-Gasperina@vcd.hp.com (Marco Dalla Gasperina)
Date: 1996/05/01
Raw View
In article <1996May1.083319.26621@iiasa.ac.at>, marek@iiasa.ac.at (Marek  MAKOWSKI) wrote:
>I would appreciate your comment on the correctness of the
>following declaration:
>
>template <class Left, class Right> class mPair {
>public:
>   mPair(const Left& left_v = (Left) 0, const Right& right_v = (Right) 0)
>                : left_(left_v), right_(right_v) {}
>                // details removed

Correct me if I'm wrong here... (like I need to say that on a news
group:-)  Consider one of the parameters: "const Left& left_v = (Left) 0"
There must be a type conversion defined for int -> Left... which in this
case is called to supply a default for the argument.  Then, a reference
to that temporary is created and a reference passed to the copy ctor
for "left_"

What's the lifetime of a temporary in this case?  The temporary may be
deleted before the copy constructor happens although I don't know if this
is a deviation from the standard or not.

Is this a sequence point question (e.g. between the parameters and the
initialization list)?

>private:
>   Left left_;
>   Right right_;
>};
>
>All my code is compiled without any warnings by Watcom, Borland,
>Symantec and SunPro compilers.
>The code that uses the above ctor run correctly with the all those
>compilers but Symantec.
>Symantec "blows-up" when the default ctor is used first time.
>
>Everything is fine also with Symantec, if the above ctor is replaced
>by the following two:
>
>   mPair() : left_((Left) 0), right_((Right) 0) {}

Here I think you're ok since the temporaries get created right at
the point they're needed and can't be destroyed prior to the
copy ctor being called.

>
>   mPair(const Left& left_v, const Right& right_v)
>      : left_(left_v), right_(right_v) {}
>
>
>Did I violate the C++ standard or is it a bug in Symantec ?
>If a C++ rule is violated then why there is no warning (or better an
>error) from any of those widely used compilers ?

I have no idea... I'd like to know myself.

>
>Thanks in advance for your time,
>Marek
>
>--
>                  Marek Makowski  | Email: marek@iiasa.ac.at
>         International Institute  | Phone: (+43-2236) 807.561
>    for Applied Systems Analysis  | Fax:   (+43-2236) 71.313
>       A-2361 Laxenburg, Austria  | Web: http://www.iiasa.ac.at

marco
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]