From: Visda on
Is a warning/error compiler message expected for the code snippet
below?

struct Student;

int main() {
struct Student *ps;
shared_ptr<Student> sps(ps);

return 0;
}

I expect front end to complain about Student not being defined and ps
not being dynamically allocated, but it doesn't.

Thanks in advance.
Visda

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 17 Jun., 09:15, Visda <visda.vokhsho...(a)gmail.com> wrote:
> Is a warning/error compiler message expected for the code snippet below?
>
> struct Student;
>
> int main() {
> struct Student *ps;
> shared_ptr<Student> sps(ps);
> return 0;
> }
>
> I expect front end to complain about Student not being defined and ps
> not being dynamically allocated, but it doesn't.

In regard to the first point ("Student not being defined") and
assuming you are referring to boost::shared_ptr or std::shared_ptr: Both
do support incomplete types, so there is no reason to reject that code.
In fact, it was one of the important development objectives, to
make that work. For example, it won't work with std::auto_ptr (but
it also works with std::unique_ptr).

In regard to your second point: There does not exist a purely
syntactic means to mark a requirement "needs dynamic allocation".
The situation becomes even more complicated, because both
shared_ptr implementations do also support the initialization with
pointer values resulting from arbitrary allocations forms (including
a no-allocation strategy, if directly binding an automatic variable
from stack or a variable with static storage duration) - it only depends
on the proper deleter type. It would require a compiler filled-up with a
lot of specialized rules of an IDE to realize such a requirement.

One thing what todays compilers will (and should) do, is to warn
about the uninitialized value of ps. E.g. Comeau online reports:

"line 11: warning: variable "ps" is used before its value is set
shared_ptr<Student> sps(ps);"

HTH & Greetings from Bremen,

Daniel Kr�gler


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ulrich Eckhardt on
Visda wrote:
> Is a warning/error compiler message expected for the code snippet
> below?
>
> struct Student;
>
> int main() {
> struct Student *ps;
> shared_ptr<Student> sps(ps);
>
> return 0;
> }
>
> I expect front end to complain about Student not being defined

Yes, I would hope for a warning there, too, because it generates code that
tries to invoke delete on an incomplete type...

> and ps not being dynamically allocated

....but not here, since it has no way of knowing whether 'ps' points to any
dynamically allocated memory.

BTW: "Student" is a known type after the first line, no need for
the "struct" in front of it later on.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Johannes Schaub (litb) on
Visda wrote:

> Is a warning/error compiler message expected for the code snippet
> below?
>
> struct Student;
>
> int main() {
> struct Student *ps;
> shared_ptr<Student> sps(ps);
>
> return 0;
> }
>
> I expect front end to complain about Student not being defined and ps
> not being dynamically allocated, but it doesn't.
>

This is not expected to work. shared_ptr requires a complete type in its
*constructor* if it is constructed with a pointer as argument.

However your type is incomplete. Other smart pointers require complete types
in their *destructor*, which is kinda more awkward because the whole point
is to don't care about destruction with smart pointers.

In pimpl classes this usually means to be forced to add empty destructors to
..cpp files just to have the implicit destructor calls to see completely
defined classes.

So the least you need to do is to provide a definitin of "Student" when you
construct the smart pointer - more precisely, when the constructor of the
smart pointer with a pointer argument is instantiated.

Your code contains undefined behavior for C++0x (probably this is why it
doesn't give you errors). The version in boost, however, gives you a
diagnostic message for the code (something like "sizeof applied to
incomplete type" or like that).


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ahmed Charles on
On Jun 17, 12:15 am, Visda <visda.vokhsho...(a)gmail.com> wrote:
> Is a warning/error compiler message expected for the code snippet
> below?
>
> struct Student;
>
> int main() {
> struct Student *ps;
> shared_ptr<Student> sps(ps);
>
> return 0;
>
> }
>
> I expect front end to complain about Student not being defined and ps
> not being dynamically allocated, but it doesn't.

shared_ptr is designed so that it doesn't require complete types, just
like regular pointers. This can be very useful.

As for the lack of ps being assigned an appropriate pointer, it's not
ill-formed. Most compilers will issue a warning because ps was used
and not initialized. That said, constructing a shared_ptr from null
pointer (0, nullptr) is well-formed, but it's not necessary since the
default constructor does the same thing.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]