473,395 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2125
>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Roger Leigh | last post by:
In a header, I have the following class template: template<typename _ListItem> class SimpleListItemConvert : public pqxxobject::RowConvert<_ListItem> { }; However, this won't compile: $...
5
by: Yoon-Soo Lee | last post by:
I am using Visual C++ .NET 2003 and running into some linking error from the following template code. The error messages is error LNK2019: unresolved external symbol "class...
5
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: ...
8
by: Douglas | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hello, The following code does not compile if line 3 is uncommented "using namespace std". I do not understand it. Could...
2
by: Ruben Campos | last post by:
I have a problem with a template function that is declared as a friend of a template class. I'll first show the exact problem with source code: // MyClass.hpp template <typename T> class...
1
by: Michael Buechel | last post by:
Hi NG, I've copied this from the article Q309801 (http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B309801). ---- schnipp --------- You may receive an LNK2019 error message when you...
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
6
by: greek_bill | last post by:
Hi, I'm interested in developing an application that needs to run on more than one operating system. Naturally, a lot of the code will be shared between the various OSs, with OS specific...
5
by: Daniel T. | last post by:
The goal is to make a friend function of an inner template class... template < typename T > class Foo { public: class Bar { friend bool operator==( const typename Foo<T>::Bar& lhs, const...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.