Topic: should this compile


Author: Alan_L_Gray@hotmail.com (Alan Gray)
Date: 1999/01/26
Raw View
In article <1Ftn2.93$jV.32@news.rdc1.wa.home.com>, larrybr@seanet.com
says...
>
>
>Alan Gray wrote in message
<77k72d$1ms$1@reader1.reader.news.ozemail.net>...
>>
>>Should the following bit of code compile.
>
>No.  Explanation inserted below.
>
>>class Lee {
>>
>>public:
>>
>>  Lee();
>>  virtual ~Lee();
>>};
>>
>>class Alan {
>....
>>};
>....
>>template<class A,class B> void DELETE(A a,B b)
>
>For reasons stated below, write instead:
>template<class A,class B> void DELETE(A *a, B *b)
>>{
>>  b->~B();                       // choke
>
>When the "class A" parameter is of type "B *",
>then "~B()" makes no sense.  Hence the compaint,
>" class does not have a destructor called '~B' ".
>
>>  ::operator delete(b,a);
>>}
>>
>>int main(int arc,char **argv)
>>{
>>  Alan *alan = new Alan;
>>  Lee  *lee  = new(alan) Lee;
>>
>>  DELETE(alan,lee);
>
>This resolves to DELETE<A *, B *>(alan, lee).
>
>--Larry Brasfield
>Above opinions may be mine alone.
>X-Replace-Address
>(Humans may reply at unundered larry_br@sea_net.com )
>
>

Thanks for that. A bit of brain fade.
Obvious once I saw the responses.

Actually both

template<class A,class B> void DELETE(A *a, B *b) {}
template<class A,class B> void DELETE(A a, B *b) {}

will compile and run.



[ 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: Alan_L_Gray@hotmail.com (Alan Gray)
Date: 1999/01/14
Raw View
Should the following bit of code compile.
I am using Microsoft Developer studio V6
It chokes on the line marked // choke


class Lee {

public:

  Lee();
  virtual ~Lee();
};

class Alan {

public:

  Alan();
  virtual ~Alan();

  void *allocate(size_t sz);
  void  deallocate(void *ptr);
};

void *operator new(size_t size,Alan *alan)
// ---------------------------------------------
// ---------------------------------------------
{
  return(alan->allocate(size));
}
void operator delete(void *ptr,Alan *alan)
// ---------------------------------------------
// ---------------------------------------------
{
  alan->deallocate(ptr);
}

template<class A,class B> void DELETE(A a,B b)
{
  b->~B();                       // choke
  ::operator delete(b,a);
}

int main(int arc,char **argv)
{
  Alan *alan = new Alan;
  Lee  *lee  = new(alan) Lee;

  DELETE(alan,lee);

  delete alan;

  return(0);
}



[ 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: 1999/01/14
Raw View
In article <77k72d$1ms$1@reader1.reader.news.ozemail.net>, Alan Gray
<Alan_L_Gray@hotmail.com> writes
>template<class A,class B> void DELETE(A a,B b)
>{
>  b->~B();                       // choke

but b is a B not a B*
>  ::operator delete(b,a);
>}


Francis Glassborow      Chair of 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              ]






Author: Larry Brasfield <larrybr@seanet.com>
Date: 1999/01/14
Raw View
Alan Gray wrote in message <77k72d$1ms$1@reader1.reader.news.ozemail.net>...
>
>Should the following bit of code compile.

No.  Explanation inserted below.

>class Lee {
>
>public:
>
>  Lee();
>  virtual ~Lee();
>};
>
>class Alan {
....
>};
....
>template<class A,class B> void DELETE(A a,B b)

For reasons stated below, write instead:
template<class A,class B> void DELETE(A *a, B *b)
>{
>  b->~B();                       // choke

When the "class A" parameter is of type "B *",
then "~B()" makes no sense.  Hence the compaint,
" class does not have a destructor called '~B' ".

>  ::operator delete(b,a);
>}
>
>int main(int arc,char **argv)
>{
>  Alan *alan = new Alan;
>  Lee  *lee  = new(alan) Lee;
>
>  DELETE(alan,lee);

This resolves to DELETE<A *, B *>(alan, lee).

--Larry Brasfield
Above opinions may be mine alone.
X-Replace-Address
(Humans may reply at unundered larry_br@sea_net.com )




[ 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: "Andrei Alexandrescu" <alexandrescua@micromodeling.com>
Date: 1999/01/14
Raw View
About my previous answer:
That code indeed shouldn't compile! I was in a hurry! Please leave me
alive!!!

Thanks,

Andrei




[ 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: "Andrei Alexandrescu" <alexandrescua@micromodeling.com>
Date: 1999/01/15
Raw View
Alan Gray wrote in message <77k72d$1ms$1@reader1.reader.news.ozemail.net>...
>Should the following bit of code compile.
>I am using Microsoft Developer studio V6
>It chokes on the line marked // choke
[snip]

Yes, it should compile. However, even if it would, I'm not sure it would do
what you want.
This is because you call DELETE with B being of type "Lee *". Hence, a
pointer's destructor (which does nothing) will be called.

I guess you wanted to destroy the object pointed to by that pointer,
something like:

template<class A,class B> void DELETE(A a,B * b)
{
  b->~B();
  ::operator delete(b,a);
}

(Note the "*" in the argument list.)
Hope this helps,

Andrei
---
[ 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              ]