Using a template to 'new' a forward declared class

O

Ole Nielsby

I want to create (with new) and delete a forward declared class.

(I'll call them Zorgs here - the real-life Zorks are platform-dependent
objects (mutexes, timestamps etc.) used by a cross-platform scripting
engine. When the scripting engine is embedded in an application, a
platform-specific support library is linked in.)

My first attempt goes here:

---code begin (library)---
class Zorg_implementation; //to be defined by another library
class Zorg {
public:
Zorg(): zi(new Zorg_implementation) {} //compile error
~Zorg() {delete zi;}
private:
Zorg_implementation *zi;
};
---code end---

which won't compile
(VC8 says: no appropriate default constructor available)

Then I stuffed it in a dummy template:

---code begin---
class Zorg_implementation; //to be defined by another library
template <typename Z> class Zorg_t {
public:
Zorg_t(): zi(new Z) {}
~Zorg_t() {delete zi;}
private:
Z *zi;
};
typedef Zorg_template<Zorg_implementation> Zorg;
---code end---

This compiles and runs fine. Zorg_wrapper instances can be created
by one library, while another library defines the Zorg_implementation class.

This strikes me as odd - I would expect the templated version to have the
same effect as the simple version.

Can anybody explain what is going on here? Is this standard behaviour
or a VC8 quirk? Can I rely on it - or should I define a factory class?

Ole Nielsby
(my reply address has a question that must be answered)
 
A

antonov84

Can anybody explain what is going on here? Is this standard behaviour
or a VC8 quirk? Can I rely on it - or should I define a factory class?

Unfortunatelly, dont know if this is standart or not, but from my
experience, templates are compiled only at a point you start using
them with concrete template params being set. This is to the extent if
you dont call some specific templated class member (directly or not),
it wont be compiled, while the rest methods you called will.
So prolly the part of your code which doesnt know
'Zorg_implementation' doesnt call Zorg_t constructor. And the code
which does has 'Zorg_implementation' fully defined.
 
J

James Kanze

I want to create (with new) and delete a forward declared class.

You can't. The type must be complete before the new expression.
(I'll call them Zorgs here - the real-life Zorks are platform-dependent
objects (mutexes, timestamps etc.) used by a cross-platform scripting
engine. When the scripting engine is embedded in an application, a
platform-specific support library is linked in.)

The usual solution here is either the compilation firewall idiom
or a factory function. (If you're new'ing the objects, a
factory function is probably preferred, but it requires all
instances to be new'ed.)
My first attempt goes here:
---code begin (library)---
class Zorg_implementation; //to be defined by another library
class Zorg {
public:
Zorg(): zi(new Zorg_implementation) {} //compile error
~Zorg() {delete zi;}
private:
Zorg_implementation *zi;};
---code end---
which won't compile
(VC8 says: no appropriate default constructor available)

More generally, "new Toto" is only legal if Toto is a complete
type.
Then I stuffed it in a dummy template:
---code begin---
class Zorg_implementation; //to be defined by another library
template <typename Z> class Zorg_t {
public:
Zorg_t(): zi(new Z) {}
~Zorg_t() {delete zi;}
private:
Z *zi;};
typedef Zorg_template<Zorg_implementation> Zorg;

I presume that Zorg_template is a typo for Zorg_t, here.
---code end---

If you never actually instantiation a Zorg, the constructor is
never instantiated, and there is no problem. If whenever you
create an instance of Zorg, Zorg_implementation has been fully
defined, there is also no problem. Otherwise, you will
encounter the same problem as above.

The main difference, of course, is that in your first version,
you will get a compiler error every time the compiler sees the
definition of Zorg (without first having seen the definition of
Zorg_implementation). In the second, you will only get a
compiler error if you try to actually create an instance of Zorg
at a point where Zorg_implementation has not been fully defined.
This compiles and runs fine. Zorg_wrapper instances can be created
by one library, while another library defines the Zorg_implementation class.
This strikes me as odd - I would expect the templated version to have the
same effect as the simple version.
Can anybody explain what is going on here? Is this standard behaviour
or a VC8 quirk? Can I rely on it - or should I define a factory class?

Without knowing more about the rest of the code, it is difficult
to say. In these sort of cases, I tend to favor factories,
however. In the exact example you show, of course, it would be
sufficient that the constructor and destructor not be inline,
and that Zorg_implementation be fully defined in the module
where they were defined.
 
O

Ole Nielsby

Unfortunatelly, dont know if this is standart or not, but from my
experience, templates are compiled only at a point you start using
them with concrete template params being set. This is to the extent if
you dont call some specific templated class member (directly or not),
it wont be compiled, while the rest methods you called will.
So prolly the part of your code which doesnt know
'Zorg_implementation' doesnt call Zorg_t constructor. And the code
which does has 'Zorg_implementation' fully defined.

This is not the case. VC8 allows me to compile Zorg-creating code
into a static library, without including the Zorg_implementation definition
at all, doing the binding at link time. I don't know why this works with
the template version but not with the simple one.
 
O

Ole Nielsby

James Kanze said:
Ole Nielsby said:
I want to create (with new) and delete a forward declared class.

You can't. The type must be complete before the new expression.
[...]
I stuffed it in a dummy template:
---code begin---
class Zorg_implementation; //to be defined by another library
template <typename Z> class Zorg_t {
public:
Zorg_t(): zi(new Z) {}
~Zorg_t() {delete zi;}
private:
Z *zi;};
typedef Zorg_t<Zorg_implementation> Zorg;
---code end---
[typo corrected]
If you never actually instantiation a Zorg, the constructor is
never instantiated, and there is no problem. If whenever you
create an instance of Zorg, Zorg_implementation has been fully
defined, there is also no problem. Otherwise, you will
encounter the same problem as above.

Well, that's the funny thing. I declare, define and use a static Zorg
instance in a static library that is compiled without ever defining
the Zorg_implementation.
[...] you will only get a compiler error if you try to actually
create an instance of Zorg at a point where Zorg_implementation
has not been fully defined.

VC8 allows just that. Though I guess it's not standard-compliant;
there seems to be an agreement here that the template instantiation
should do the same as if I had defined the stuff non-templatewise
at the point of the template instantiation.

(I suspect the VC8 compiler for being "infected" with .NET
generics under the hood... perhaps the compiler tries to reduce
templates to CLR generics, and deduces from the code that the
template argument should have a 'new' constraint. But this is
pure off-topic speculation.)
 
A

antonov84

This is not the case. VC8 allows me to compile Zorg-creating code
into a static library, without including the Zorg_implementation definition
at all, doing the binding at link time. I don't know why this works with
the template version but not with the simple one.

Well, post the creating code then (which instantiates an object of
Zorg_t with no zorg_implementation defined, and rests in your static
lib cpp file).
 
J

James Kanze

James Kanze said:
Ole Nielsby said:
I want to create (with new) and delete a forward declared class.
You can't. The type must be complete before the new expression.
[...]
I stuffed it in a dummy template:
---code begin---
class Zorg_implementation; //to be defined by another library
template <typename Z> class Zorg_t {
public:
Zorg_t(): zi(new Z) {}
~Zorg_t() {delete zi;}
private:
Z *zi;};
typedef Zorg_t<Zorg_implementation> Zorg;
---code end---
[typo corrected]
If you never actually instantiation a Zorg, the constructor is
never instantiated, and there is no problem. If whenever you
create an instance of Zorg, Zorg_implementation has been fully
defined, there is also no problem. Otherwise, you will
encounter the same problem as above.
Well, that's the funny thing. I declare, define and use a static Zorg
instance in a static library that is compiled without ever defining
the Zorg_implementation.

That is strange. Formally, until you actually finish linking,
you are still "compiling", and compiler can (and some do) defer
instantiation until link time (and the code isn't linked when
you build a static library). Doing so systematically, today, is
very rare, however, and all of the compilers I use regularly
will attempt instantiation at compile time, if the definition is
available (which it is here). And there is no doubt that "new
Zorg_implementation" is illegal if Zorg_implementation is an
incomplete type, and every compiler I know will complain about
it.
[...] you will only get a compiler error if you try to actually
create an instance of Zorg at a point where Zorg_implementation
has not been fully defined.
VC8 allows just that. Though I guess it's not standard-compliant;
there seems to be an agreement here that the template instantiation
should do the same as if I had defined the stuff non-templatewise
at the point of the template instantiation.

I'm not sure with regards to standard compliance; it might be
undefined behavior when a template is involved. But I couldn't
reproduce your symptoms with any of the compilers I have at
hand, including VC8. All of them compiled the code above
without problems, of course, because there was no instantiation.
But with all of them, including VC8, I get an error message if I
add the line:

Zorg aZorg ;

at the end. So either you're actually doing something
different, and I've misunderstood what you're trying, or you're
using some special options with VC8 (in addition to the ones you
need to compile any C++, like /vmg /GR /EHs).
(I suspect the VC8 compiler for being "infected" with .NET
generics under the hood... perhaps the compiler tries to reduce
templates to CLR generics, and deduces from the code that the
template argument should have a 'new' constraint. But this is
pure off-topic speculation.)

That's not the case with the free, downloadable version, at
least with the command line options I use. I'd almost be
willing to bet that either you've actually furnished a
definition of Zorg_implementation, or more likely, that you've
not done anything to trigger instantiation of the template.
(Note that just declaring an instance, without defining it, or
defining a pointer to an instance, does not trigger
instantiation.)
 
Z

Zeppe

Ole said:
Well, that's the funny thing. I declare, define and use a static Zorg
instance in a static library that is compiled without ever defining
the Zorg_implementation.

So, you are using

Zorg_t<Foo> bar;

and using bar, in the library, Foo not being defined? It seems very
strange to me as well. And, actually, I'm unable to reproduce this
behaviour. Can you post a minimal example of a library that uses Zorg
object with undefined template type and compiles fine?

Regards,

Zeppe
 
O

Ole Nielsby

I said:
Well, that's the funny thing. I declare, define and use a static Zorg
instance in a static library that is compiled without ever defining
the Zorg_implementation.

Sorry - on closer inspection, the static Zorg singleton is declared and
used in the static library, but it is defined in another library which has
Zorg_implementation defined.

So it turns out, the template class constructor is not instantiated by
the declaration of the static singleton, only by its definition. I got
put off by the workings of lazy code generation...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top