473,586 Members | 2,695 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_implementa tion; //to be defined by another library
class Zorg {
public:
Zorg(): zi(new Zorg_implementa tion) {} //compile error
~Zorg() {delete zi;}
private:
Zorg_implementa tion *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_implementa tion; //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<Z org_implementat ionZorg;
---code end---

This compiles and runs fine. Zorg_wrapper instances can be created
by one library, while another library defines the Zorg_implementa tion 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 2136
>
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_implement ation' doesnt call Zorg_t constructor. And the code
which does has 'Zorg_implement ation' fully defined.

May 20 '07 #2
On May 20, 9:08 pm, "Ole Nielsby" <ole.nielsby@te kare-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_implementa tion; //to be defined by another library
class Zorg {
public:
Zorg(): zi(new Zorg_implementa tion) {} //compile error
~Zorg() {delete zi;}
private:
Zorg_implementa tion *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_implementa tion; //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<Z org_implementat ionZorg;
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_implementa tion 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_implementa tion). 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_implementa tion 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_implementa tion 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_implementa tion be fully defined in the module
where they were defined.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
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_implement ation' doesnt call Zorg_t constructor. And the code
which does has 'Zorg_implement ation' fully defined.
This is not the case. VC8 allows me to compile Zorg-creating code
into a static library, without including the Zorg_implementa tion 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*********@gm ail.comwrote:
Ole Nielsby <ole.nielsby@te kare-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_implementa tion; //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_imp lementationZorg ;
---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_implementa tion 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_implementa tion.
[...] you will only get a compiler error if you try to actually
create an instance of Zorg at a point where Zorg_implementa tion
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_implementa tion 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_implementa tion defined, and rests in your static
lib cpp file).

May 21 '07 #6
On May 21, 3:10 am, "Ole Nielsby" <ole.niel...@te kare-you-
spamminglogisk. dkwrote:
James Kanze <james.ka...@gm ail.comwrote:
Ole Nielsby <ole.nielsby@te kare-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_implementa tion; //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_imp lementationZorg ;
---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_implementa tion 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_implementa tion.
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_implementa tion" is illegal if Zorg_implementa tion 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_implementa tion
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_implementa tion, 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 objektorientier ter Datenverarbeitu ng
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*********@gm ail.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_implementa tion 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_implementa tion.
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_implementa tion.
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_implementa tion 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
1618
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: $ g++ -c simplelist.cc -o simplelist.o
5
8440
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 std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Test<int> &)"...
5
2694
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: //**************************************************** // testClass.h //**************************************************** #ifndef TESTCLASS_H
8
4365
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 somebody explain it to me? I am using MSVC 6.0. Thanks a lot,
2
3971
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 MyClass { // ... friend void MyFriendFunction (MyClass <T> * const ptrMyClass); // ...
1
3609
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 build a Visual C++ 2005 or Visual C++ .NET application that uses a CString-derived class from a DLL file ---- schnapp ---------
0
2023
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 this topic getting lost before people interested in the problem notice it. The important tricks to the solution are two: 1) make the custom classes...
6
2141
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 functionality being kept separate. I've been going over the various approaches I could follow in order to implement the OS-specific functionality. The...
5
2445
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 typename Foo<T>::Bar& rhs); }; };
0
7908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8336
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8212
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5389
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2343
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.