Topic: Q: diagnostic required for private but optimised out copy ctor?


Author: Ben Liblit <ben@ls.com>
Date: 1996/12/13
Raw View
mishad@iplbath.com (Misha Dorman) writes:
> is a diagnostic required (because the copy ctor is private) even
> though the compiler can optimise it out.

My reading of the April '95 DWP, section 12.2 [class.temporary],
paragraph 1 is that a diagnostic is required:

    Even when the creation of the temporary object is avoided, all the
    semantic restrictions must be respected as if the temporary object
    was created.  [ Example: even if the copy constructor is not
    called, all the semantic restrictions, such as accessibility,
    shall be satisfied. ]

In the example you gave, the semantic restriction of an accessibe copy
constructor is not satisfied.  I assume that failure to conform with a
"shall" directive in the DWP means that a diagnostic must be issued.

The compiler that I use day-to-day gives only a warning, though it
should probably be an error.  The actual message is one of the most
bizarre I have ever seen:

    "try.C", line 12: warning(3134): "X::X(const X &)", required for
        copy that was eliminated, is inaccessible

How's that for specific?
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: jpotter@falcon.lhup.edu (John E. Potter)
Date: 1996/12/18
Raw View
Ben Liblit (ben@ls.com) wrote:
: mishad@iplbath.com (Misha Dorman) writes:
: > is a diagnostic required (because the copy ctor is private) even
: > though the compiler can optimise it out.
: My reading of the April '95 DWP, section 12.2 [class.temporary],
: paragraph 1 is that a diagnostic is required:
:     Even when the creation of the temporary object is avoided, all the
:     semantic restrictions must be respected as if the temporary object
:     was created.  [ Example: even if the copy constructor is not
:     called, all the semantic restrictions, such as accessibility,
:     shall be satisfied. ]
: In the example you gave, the semantic restriction of an accessibe copy
: constructor is not satisfied.  I assume that failure to conform with a
: "shall" directive in the DWP means that a diagnostic must be issued.

But, if we flip a few pages in that document to 12.8 [class.copy] /15
 Whenever a class object is copied and the implementation can prove
 that either the original or the copy will never again be used, an
 implementation is permitted to treat the original and the copy as
 two different ways of refering to the same object and not perform
 a copy at all.
The sample g(t2) looks just like the posted code and it is trivial to
prove that the unnamed temp will never again be used.  Also note the
absence of any wording requiring accessibility of copy constructor.
Compilers have been quietly doing this since cfront.  However, it
seems that an optimization could change the well-formedness of a
program.

This question seems to come up quite often, and I have yet to see a
definitive answer.  Knowing that the wording of the CD2 has changed,
could someone with current knowledge give a ruling?

: The compiler that I use day-to-day gives only a warning, though it
: should probably be an error.  The actual message is one of the most
: bizarre I have ever seen:
:     "try.C", line 12: warning(3134): "X::X(const X &)", required for
:         copy that was eliminated, is inaccessible
: How's that for specific?

Humm.  If the code is ill-formed, a diagnostic is required, and a
warning is a diagnostic.  If the code is well-formed, an executable
is required, and it gives one.  Looks like they conform either way,
smart vendor.

John
---
[ 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: mishad@iplbath.com (Misha Dorman)
Date: 1996/12/10
Raw View
Given the following code:

> struct X
>  {
>  X(int);  // public ctor
> private:
>  X(const X&); // private copy ctor
>  };
>
> extern void foo(X);
>
> main()
> {
> foo( X(1) );  // func-style cast, then pass-by-value
> }

is a diagnostic required (because the copy ctor is private) even though the
compiler can optimise it out. I assume it is (in the same way that it is
for

> X x = X(1);

Can anyone confirm this?

--
Misha Dorman                             email: mishad@iplbath.com
Software Products Group                  WWW:   http://www.iplbath.com
IPL Information Processing Ltd           Tel:   +44 (0)1225 444888
Eveleigh House, Grove Street, Bath BA1 5LR, England
---
[ 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
]