469,600 Members | 2,444 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

Using a template to 'new' a forward declared class

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 Zclass Zorg_t {
public:
Zorg_t(): zi(new Z) {}
~Zorg_t() {delete zi;}
private:
Z *zi;
};
typedef Zorg_template<Zorg_implementationZorg;
---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)
May 20 '07 #1
8 1879
>
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.

May 20 '07 #2
On May 20, 9:08 pm, "Ole Nielsby" <ole.nielsby@tekare-you-a-spammer?
logisk.dkwrote:
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 Zclass Zorg_t {
public:
Zorg_t(): zi(new Z) {}
~Zorg_t() {delete zi;}
private:
Z *zi;};
typedef Zorg_template<Zorg_implementationZorg;
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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 20 '07 #3
<an*******@ukr.netwrote:
>
>>
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.
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.
May 21 '07 #4
James Kanze <ja*********@gmail.comwrote:
Ole Nielsby <ole.nielsby@tekare-you-a-spammer?logisk.dkwrote:
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 Zclass Zorg_t {
public:
Zorg_t(): zi(new Z) {}
~Zorg_t() {delete zi;}
private:
Z *zi;};
typedef Zorg_t<Zorg_implementationZorg;
---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.)
May 21 '07 #5
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).

May 21 '07 #6
On May 21, 3:10 am, "Ole Nielsby" <ole.niel...@tekare-you-
spamminglogisk.dkwrote:
James Kanze <james.ka...@gmail.comwrote:
Ole Nielsby <ole.nielsby@tekare-you-a-spammer?logisk.dkwrote:
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 Zclass Zorg_t {
public:
Zorg_t(): zi(new Z) {}
~Zorg_t() {delete zi;}
private:
Z *zi;};
typedef Zorg_t<Zorg_implementationZorg;
---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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 21 '07 #7
Ole Nielsby wrote:
James Kanze <ja*********@gmail.comwrote:
>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.
So, you are using

Zorg_t<Foobar;

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
May 21 '07 #8
I wrote:
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...
May 21 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Roger Leigh | last post: by
5 posts views Thread by Trevor Lango | last post: by
8 posts views Thread by Douglas | last post: by
2 posts views Thread by Ruben Campos | last post: by
5 posts views Thread by Daniel T. | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.